1 /* vi:set ts=8 sts=4 sw=4: 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_pattern; /* search pattern for the error */ 37 char_u *qf_text; /* description of the error */ 38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */ 39 char_u qf_cleared; /* set to TRUE if line has been deleted */ 40 char_u qf_type; /* type of the error (mostly 'E'); 1 for 41 :helpgrep */ 42 char_u qf_valid; /* valid error message detected */ 43 }; 44 45 /* 46 * There is a stack of error lists. 47 */ 48 #define LISTCOUNT 10 49 50 typedef struct qf_list_S 51 { 52 qfline_T *qf_start; /* pointer to the first error */ 53 qfline_T *qf_last; /* pointer to the last error */ 54 qfline_T *qf_ptr; /* pointer to the current error */ 55 int qf_count; /* number of errors (0 means no error list) */ 56 int qf_index; /* current index in the error list */ 57 int qf_nonevalid; /* TRUE if not a single valid entry found */ 58 char_u *qf_title; /* title derived from the command that created 59 * the error list */ 60 } qf_list_T; 61 62 struct qf_info_S 63 { 64 /* 65 * Count of references to this list. Used only for location lists. 66 * When a location list window reference this list, qf_refcount 67 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount 68 * reaches 0, the list is freed. 69 */ 70 int qf_refcount; 71 int qf_listcount; /* current number of lists */ 72 int qf_curlist; /* current error list */ 73 qf_list_T qf_lists[LISTCOUNT]; 74 75 int qf_dir_curlist; /* error list for qf_dir_stack */ 76 struct dir_stack_T *qf_dir_stack; 77 char_u *qf_directory; 78 struct dir_stack_T *qf_file_stack; 79 char_u *qf_currfile; 80 int qf_multiline; 81 int qf_multiignore; 82 int qf_multiscan; 83 }; 84 85 static qf_info_T ql_info; /* global quickfix list */ 86 87 #define FMT_PATTERNS 10 /* maximum number of % recognized */ 88 89 /* 90 * Structure used to hold the info of one part of 'errorformat' 91 */ 92 typedef struct efm_S efm_T; 93 struct efm_S 94 { 95 regprog_T *prog; /* pre-formatted part of 'errorformat' */ 96 efm_T *next; /* pointer to next (NULL if last) */ 97 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */ 98 char_u prefix; /* prefix of this format line: */ 99 /* 'D' enter directory */ 100 /* 'X' leave directory */ 101 /* 'A' start of multi-line message */ 102 /* 'E' error message */ 103 /* 'W' warning message */ 104 /* 'I' informational message */ 105 /* 'C' continuation line */ 106 /* 'Z' end of multi-line message */ 107 /* 'G' general, unspecific message */ 108 /* 'P' push file (partial) message */ 109 /* 'Q' pop/quit file (partial) message */ 110 /* 'O' overread (partial) message */ 111 char_u flags; /* additional flags given in prefix */ 112 /* '-' do not include this line */ 113 /* '+' include whole line in message */ 114 int conthere; /* %> used */ 115 }; 116 117 static int qf_init_ext(qf_info_T *qi, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title); 118 static void qf_store_title(qf_info_T *qi, char_u *title); 119 static void qf_new_list(qf_info_T *qi, char_u *qf_title); 120 static void ll_free_all(qf_info_T **pqi); 121 static int qf_add_entry(qf_info_T *qi, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid); 122 static qf_info_T *ll_new_list(void); 123 static void qf_msg(qf_info_T *qi); 124 static void qf_free(qf_info_T *qi, int idx); 125 static char_u *qf_types(int, int); 126 static int qf_get_fnum(qf_info_T *qi, char_u *, char_u *); 127 static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack); 128 static char_u *qf_pop_dir(struct dir_stack_T **); 129 static char_u *qf_guess_filepath(qf_info_T *qi, char_u *); 130 static void qf_fmt_text(char_u *text, char_u *buf, int bufsize); 131 static void qf_clean_dir_stack(struct dir_stack_T **); 132 #ifdef FEAT_WINDOWS 133 static int qf_win_pos_update(qf_info_T *qi, int old_qf_index); 134 static int is_qf_win(win_T *win, qf_info_T *qi); 135 static win_T *qf_find_win(qf_info_T *qi); 136 static buf_T *qf_find_buf(qf_info_T *qi); 137 static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last); 138 static void qf_set_title_var(qf_info_T *qi); 139 static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last); 140 #endif 141 static char_u *get_mef_name(void); 142 static void restore_start_dir(char_u *dirname_start); 143 static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir); 144 static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start); 145 static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start); 146 static qf_info_T *ll_get_or_alloc_list(win_T *); 147 148 /* Quickfix window check helper macro */ 149 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL) 150 /* Location list window check helper macro */ 151 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL) 152 /* 153 * Return location list for window 'wp' 154 * For location list window, return the referenced location list 155 */ 156 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist) 157 158 /* 159 * Read the errorfile "efile" into memory, line by line, building the error 160 * list. Set the error list's title to qf_title. 161 * Return -1 for error, number of errors for success. 162 */ 163 int 164 qf_init( 165 win_T *wp, 166 char_u *efile, 167 char_u *errorformat, 168 int newlist, /* TRUE: start a new error list */ 169 char_u *qf_title) 170 { 171 qf_info_T *qi = &ql_info; 172 173 if (wp != NULL) 174 { 175 qi = ll_get_or_alloc_list(wp); 176 if (qi == NULL) 177 return FAIL; 178 } 179 180 return qf_init_ext(qi, efile, curbuf, NULL, errorformat, newlist, 181 (linenr_T)0, (linenr_T)0, 182 qf_title); 183 } 184 185 /* 186 * Maximum number of bytes allowed per line while reading a errorfile. 187 */ 188 #define LINE_MAXLEN 4096 189 190 static struct fmtpattern 191 { 192 char_u convchar; 193 char *pattern; 194 } fmt_pat[FMT_PATTERNS] = 195 { 196 {'f', ".\\+"}, /* only used when at end */ 197 {'n', "\\d\\+"}, 198 {'l', "\\d\\+"}, 199 {'c', "\\d\\+"}, 200 {'t', "."}, 201 {'m', ".\\+"}, 202 {'r', ".*"}, 203 {'p', "[- .]*"}, 204 {'v', "\\d\\+"}, 205 {'s', ".\\+"} 206 }; 207 208 /* 209 * Converts a 'errorformat' string to regular expression pattern 210 */ 211 static int 212 efm_to_regpat( 213 char_u *efm, 214 int len, 215 efm_T *fmt_ptr, 216 char_u *regpat, 217 char_u *errmsg) 218 { 219 char_u *ptr; 220 char_u *efmp; 221 char_u *srcptr; 222 int round; 223 int idx = 0; 224 225 /* 226 * Build regexp pattern from current 'errorformat' option 227 */ 228 ptr = regpat; 229 *ptr++ = '^'; 230 round = 0; 231 for (efmp = efm; efmp < efm + len; ++efmp) 232 { 233 if (*efmp == '%') 234 { 235 ++efmp; 236 for (idx = 0; idx < FMT_PATTERNS; ++idx) 237 if (fmt_pat[idx].convchar == *efmp) 238 break; 239 if (idx < FMT_PATTERNS) 240 { 241 if (fmt_ptr->addr[idx]) 242 { 243 sprintf((char *)errmsg, 244 _("E372: Too many %%%c in format string"), *efmp); 245 EMSG(errmsg); 246 return -1; 247 } 248 if ((idx 249 && idx < 6 250 && vim_strchr((char_u *)"DXOPQ", 251 fmt_ptr->prefix) != NULL) 252 || (idx == 6 253 && vim_strchr((char_u *)"OPQ", 254 fmt_ptr->prefix) == NULL)) 255 { 256 sprintf((char *)errmsg, 257 _("E373: Unexpected %%%c in format string"), *efmp); 258 EMSG(errmsg); 259 return -1; 260 } 261 fmt_ptr->addr[idx] = (char_u)++round; 262 *ptr++ = '\\'; 263 *ptr++ = '('; 264 #ifdef BACKSLASH_IN_FILENAME 265 if (*efmp == 'f') 266 { 267 /* Also match "c:" in the file name, even when 268 * checking for a colon next: "%f:". 269 * "\%(\a:\)\=" */ 270 STRCPY(ptr, "\\%(\\a:\\)\\="); 271 ptr += 10; 272 } 273 #endif 274 if (*efmp == 'f' && efmp[1] != NUL) 275 { 276 if (efmp[1] != '\\' && efmp[1] != '%') 277 { 278 /* A file name may contain spaces, but this isn't 279 * in "\f". For "%f:%l:%m" there may be a ":" in 280 * the file name. Use ".\{-1,}x" instead (x is 281 * the next character), the requirement that :999: 282 * follows should work. */ 283 STRCPY(ptr, ".\\{-1,}"); 284 ptr += 7; 285 } 286 else 287 { 288 /* File name followed by '\\' or '%': include as 289 * many file name chars as possible. */ 290 STRCPY(ptr, "\\f\\+"); 291 ptr += 4; 292 } 293 } 294 else 295 { 296 srcptr = (char_u *)fmt_pat[idx].pattern; 297 while ((*ptr = *srcptr++) != NUL) 298 ++ptr; 299 } 300 *ptr++ = '\\'; 301 *ptr++ = ')'; 302 } 303 else if (*efmp == '*') 304 { 305 if (*++efmp == '[' || *efmp == '\\') 306 { 307 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */ 308 { 309 if (efmp[1] == '^') 310 *ptr++ = *++efmp; 311 if (efmp < efm + len) 312 { 313 *ptr++ = *++efmp; /* could be ']' */ 314 while (efmp < efm + len 315 && (*ptr++ = *++efmp) != ']') 316 /* skip */; 317 if (efmp == efm + len) 318 { 319 EMSG(_("E374: Missing ] in format string")); 320 return -1; 321 } 322 } 323 } 324 else if (efmp < efm + len) /* %*\D, %*\s etc. */ 325 *ptr++ = *++efmp; 326 *ptr++ = '\\'; 327 *ptr++ = '+'; 328 } 329 else 330 { 331 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */ 332 sprintf((char *)errmsg, 333 _("E375: Unsupported %%%c in format string"), *efmp); 334 EMSG(errmsg); 335 return -1; 336 } 337 } 338 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL) 339 *ptr++ = *efmp; /* regexp magic characters */ 340 else if (*efmp == '#') 341 *ptr++ = '*'; 342 else if (*efmp == '>') 343 fmt_ptr->conthere = TRUE; 344 else if (efmp == efm + 1) /* analyse prefix */ 345 { 346 if (vim_strchr((char_u *)"+-", *efmp) != NULL) 347 fmt_ptr->flags = *efmp++; 348 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL) 349 fmt_ptr->prefix = *efmp; 350 else 351 { 352 sprintf((char *)errmsg, 353 _("E376: Invalid %%%c in format string prefix"), *efmp); 354 EMSG(errmsg); 355 return -1; 356 } 357 } 358 else 359 { 360 sprintf((char *)errmsg, 361 _("E377: Invalid %%%c in format string"), *efmp); 362 EMSG(errmsg); 363 return -1; 364 } 365 } 366 else /* copy normal character */ 367 { 368 if (*efmp == '\\' && efmp + 1 < efm + len) 369 ++efmp; 370 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL) 371 *ptr++ = '\\'; /* escape regexp atoms */ 372 if (*efmp) 373 *ptr++ = *efmp; 374 } 375 } 376 *ptr++ = '$'; 377 *ptr = NUL; 378 379 return 0; 380 } 381 382 static void 383 free_efm_list(efm_T **efm_first) 384 { 385 efm_T *efm_ptr; 386 387 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first) 388 { 389 *efm_first = efm_ptr->next; 390 vim_regfree(efm_ptr->prog); 391 vim_free(efm_ptr); 392 } 393 } 394 395 /* Parse 'errorformat' option */ 396 static efm_T * 397 parse_efm_option(char_u *efm) 398 { 399 char_u *errmsg = NULL; 400 int errmsglen; 401 efm_T *fmt_ptr = NULL; 402 efm_T *fmt_first = NULL; 403 efm_T *fmt_last = NULL; 404 char_u *fmtstr = NULL; 405 int len; 406 int i; 407 int round; 408 409 errmsglen = CMDBUFFSIZE + 1; 410 errmsg = alloc_id(errmsglen, aid_qf_errmsg); 411 if (errmsg == NULL) 412 goto parse_efm_end; 413 414 /* 415 * Get some space to modify the format string into. 416 */ 417 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2); 418 for (round = FMT_PATTERNS; round > 0; ) 419 i += (int)STRLEN(fmt_pat[--round].pattern); 420 #ifdef COLON_IN_FILENAME 421 i += 12; /* "%f" can become twelve chars longer */ 422 #else 423 i += 2; /* "%f" can become two chars longer */ 424 #endif 425 if ((fmtstr = alloc(i)) == NULL) 426 goto parse_efm_error; 427 428 while (efm[0] != NUL) 429 { 430 /* 431 * Allocate a new eformat structure and put it at the end of the list 432 */ 433 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T)); 434 if (fmt_ptr == NULL) 435 goto parse_efm_error; 436 if (fmt_first == NULL) /* first one */ 437 fmt_first = fmt_ptr; 438 else 439 fmt_last->next = fmt_ptr; 440 fmt_last = fmt_ptr; 441 442 /* 443 * Isolate one part in the 'errorformat' option 444 */ 445 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len) 446 if (efm[len] == '\\' && efm[len + 1] != NUL) 447 ++len; 448 449 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1) 450 goto parse_efm_error; 451 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL) 452 goto parse_efm_error; 453 /* 454 * Advance to next part 455 */ 456 efm = skip_to_option_part(efm + len); /* skip comma and spaces */ 457 } 458 459 if (fmt_first == NULL) /* nothing found */ 460 EMSG(_("E378: 'errorformat' contains no pattern")); 461 462 goto parse_efm_end; 463 464 parse_efm_error: 465 free_efm_list(&fmt_first); 466 467 parse_efm_end: 468 vim_free(fmtstr); 469 vim_free(errmsg); 470 471 return fmt_first; 472 } 473 474 enum { 475 QF_FAIL = 0, 476 QF_OK = 1, 477 QF_END_OF_INPUT = 2, 478 QF_NOMEM = 3 479 }; 480 481 typedef struct { 482 char_u *linebuf; 483 int linelen; 484 char_u *growbuf; 485 int growbufsiz; 486 FILE *fd; 487 typval_T *tv; 488 char_u *p_str; 489 listitem_T *p_li; 490 buf_T *buf; 491 linenr_T buflnum; 492 linenr_T lnumlast; 493 } qfstate_T; 494 495 static char_u * 496 qf_grow_linebuf(qfstate_T *state, int newsz) 497 { 498 /* 499 * If the line exceeds LINE_MAXLEN exclude the last 500 * byte since it's not a NL character. 501 */ 502 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz; 503 if (state->growbuf == NULL) 504 { 505 state->growbuf = alloc(state->linelen + 1); 506 if (state->growbuf == NULL) 507 return NULL; 508 state->growbufsiz = state->linelen; 509 } 510 else if (state->linelen > state->growbufsiz) 511 { 512 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1); 513 if (state->growbuf == NULL) 514 return NULL; 515 state->growbufsiz = state->linelen; 516 } 517 return state->growbuf; 518 } 519 520 /* 521 * Get the next string (separated by newline) from state->p_str. 522 */ 523 static int 524 qf_get_next_str_line(qfstate_T *state) 525 { 526 /* Get the next line from the supplied string */ 527 char_u *p_str = state->p_str; 528 char_u *p; 529 int len; 530 531 if (*p_str == NUL) /* Reached the end of the string */ 532 return QF_END_OF_INPUT; 533 534 p = vim_strchr(p_str, '\n'); 535 if (p != NULL) 536 len = (int)(p - p_str) + 1; 537 else 538 len = (int)STRLEN(p_str); 539 540 if (len > IOSIZE - 2) 541 { 542 state->linebuf = qf_grow_linebuf(state, len); 543 if (state->linebuf == NULL) 544 return QF_NOMEM; 545 } 546 else 547 { 548 state->linebuf = IObuff; 549 state->linelen = len; 550 } 551 vim_strncpy(state->linebuf, p_str, state->linelen); 552 553 /* 554 * Increment using len in order to discard the rest of the 555 * line if it exceeds LINE_MAXLEN. 556 */ 557 p_str += len; 558 state->p_str = p_str; 559 560 return QF_OK; 561 } 562 563 /* 564 * Get the next string from state->p_Li. 565 */ 566 static int 567 qf_get_next_list_line(qfstate_T *state) 568 { 569 listitem_T *p_li = state->p_li; 570 int len; 571 572 while (p_li != NULL 573 && (p_li->li_tv.v_type != VAR_STRING 574 || p_li->li_tv.vval.v_string == NULL)) 575 p_li = p_li->li_next; /* Skip non-string items */ 576 577 if (p_li == NULL) /* End of the list */ 578 { 579 state->p_li = NULL; 580 return QF_END_OF_INPUT; 581 } 582 583 len = (int)STRLEN(p_li->li_tv.vval.v_string); 584 if (len > IOSIZE - 2) 585 { 586 state->linebuf = qf_grow_linebuf(state, len); 587 if (state->linebuf == NULL) 588 return QF_NOMEM; 589 } 590 else 591 { 592 state->linebuf = IObuff; 593 state->linelen = len; 594 } 595 596 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen); 597 598 state->p_li = p_li->li_next; /* next item */ 599 return QF_OK; 600 } 601 602 /* 603 * Get the next string from state->buf. 604 */ 605 static int 606 qf_get_next_buf_line(qfstate_T *state) 607 { 608 char_u *p_buf = NULL; 609 int len; 610 611 /* Get the next line from the supplied buffer */ 612 if (state->buflnum > state->lnumlast) 613 return QF_END_OF_INPUT; 614 615 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE); 616 state->buflnum += 1; 617 618 len = (int)STRLEN(p_buf); 619 if (len > IOSIZE - 2) 620 { 621 state->linebuf = qf_grow_linebuf(state, len); 622 if (state->linebuf == NULL) 623 return QF_NOMEM; 624 } 625 else 626 { 627 state->linebuf = IObuff; 628 state->linelen = len; 629 } 630 vim_strncpy(state->linebuf, p_buf, state->linelen); 631 632 return QF_OK; 633 } 634 635 /* 636 * Get the next string from file state->fd. 637 */ 638 static int 639 qf_get_next_file_line(qfstate_T *state) 640 { 641 int discard; 642 int growbuflen; 643 644 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL) 645 return QF_END_OF_INPUT; 646 647 discard = FALSE; 648 state->linelen = (int)STRLEN(IObuff); 649 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n' 650 #ifdef USE_CRNL 651 || IObuff[state->linelen - 1] == '\r' 652 #endif 653 )) 654 { 655 /* 656 * The current line exceeds IObuff, continue reading using 657 * growbuf until EOL or LINE_MAXLEN bytes is read. 658 */ 659 if (state->growbuf == NULL) 660 { 661 state->growbufsiz = 2 * (IOSIZE - 1); 662 state->growbuf = alloc(state->growbufsiz); 663 if (state->growbuf == NULL) 664 return QF_NOMEM; 665 } 666 667 /* Copy the read part of the line, excluding null-terminator */ 668 memcpy(state->growbuf, IObuff, IOSIZE - 1); 669 growbuflen = state->linelen; 670 671 for (;;) 672 { 673 if (fgets((char *)state->growbuf + growbuflen, 674 state->growbufsiz - growbuflen, state->fd) == NULL) 675 break; 676 state->linelen = (int)STRLEN(state->growbuf + growbuflen); 677 growbuflen += state->linelen; 678 if ((state->growbuf)[growbuflen - 1] == '\n' 679 #ifdef USE_CRNL 680 || (state->growbuf)[growbuflen - 1] == '\r' 681 #endif 682 ) 683 break; 684 if (state->growbufsiz == LINE_MAXLEN) 685 { 686 discard = TRUE; 687 break; 688 } 689 690 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN 691 ? 2 * state->growbufsiz : LINE_MAXLEN; 692 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz); 693 if (state->growbuf == NULL) 694 return QF_NOMEM; 695 } 696 697 while (discard) 698 { 699 /* 700 * The current line is longer than LINE_MAXLEN, continue 701 * reading but discard everything until EOL or EOF is 702 * reached. 703 */ 704 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL 705 || (int)STRLEN(IObuff) < IOSIZE - 1 706 || IObuff[IOSIZE - 1] == '\n' 707 #ifdef USE_CRNL 708 || IObuff[IOSIZE - 1] == '\r' 709 #endif 710 ) 711 break; 712 } 713 714 state->linebuf = state->growbuf; 715 state->linelen = growbuflen; 716 } 717 else 718 state->linebuf = IObuff; 719 720 return QF_OK; 721 } 722 723 /* 724 * Get the next string from a file/buffer/list/string. 725 */ 726 static int 727 qf_get_nextline(qfstate_T *state) 728 { 729 int status = QF_FAIL; 730 731 if (state->fd == NULL) 732 { 733 if (state->tv != NULL) 734 { 735 if (state->tv->v_type == VAR_STRING) 736 /* Get the next line from the supplied string */ 737 status = qf_get_next_str_line(state); 738 else if (state->tv->v_type == VAR_LIST) 739 /* Get the next line from the supplied list */ 740 status = qf_get_next_list_line(state); 741 } 742 else 743 /* Get the next line from the supplied buffer */ 744 status = qf_get_next_buf_line(state); 745 } 746 else 747 /* Get the next line from the supplied file */ 748 status = qf_get_next_file_line(state); 749 750 if (status != QF_OK) 751 return status; 752 753 /* remove newline/CR from the line */ 754 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n') 755 state->linebuf[state->linelen - 1] = NUL; 756 #ifdef USE_CRNL 757 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\r') 758 state->linebuf[state->linelen - 1] = NUL; 759 #endif 760 761 #ifdef FEAT_MBYTE 762 remove_bom(state->linebuf); 763 #endif 764 765 return QF_OK; 766 } 767 768 /* 769 * Read the errorfile "efile" into memory, line by line, building the error 770 * list. 771 * Alternative: when "efile" is NULL read errors from buffer "buf". 772 * Alternative: when "tv" is not NULL get errors from the string or list. 773 * Always use 'errorformat' from "buf" if there is a local value. 774 * Then "lnumfirst" and "lnumlast" specify the range of lines to use. 775 * Set the title of the list to "qf_title". 776 * Return -1 for error, number of errors for success. 777 */ 778 static int 779 qf_init_ext( 780 qf_info_T *qi, 781 char_u *efile, 782 buf_T *buf, 783 typval_T *tv, 784 char_u *errorformat, 785 int newlist, /* TRUE: start a new error list */ 786 linenr_T lnumfirst, /* first line number to use */ 787 linenr_T lnumlast, /* last line number to use */ 788 char_u *qf_title) 789 { 790 char_u *namebuf; 791 char_u *errmsg; 792 int errmsglen; 793 char_u *pattern; 794 qfstate_T state = {NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, 795 NULL, 0, 0}; 796 int col = 0; 797 char_u use_viscol = FALSE; 798 int type = 0; 799 int valid; 800 long lnum = 0L; 801 int enr = 0; 802 #ifdef FEAT_WINDOWS 803 qfline_T *old_last = NULL; 804 #endif 805 static efm_T *fmt_first = NULL; 806 efm_T *fmt_ptr; 807 efm_T *fmt_start = NULL; 808 char_u *efm; 809 static char_u *last_efm = NULL; 810 char_u *ptr; 811 int len; 812 int i; 813 int idx = 0; 814 int retval = -1; /* default: return error flag */ 815 int status; 816 char_u *tail = NULL; 817 regmatch_T regmatch; 818 819 namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf); 820 errmsglen = CMDBUFFSIZE + 1; 821 errmsg = alloc_id(errmsglen, aid_qf_errmsg); 822 pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern); 823 if (namebuf == NULL || errmsg == NULL || pattern == NULL) 824 goto qf_init_end; 825 826 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL) 827 { 828 EMSG2(_(e_openerrf), efile); 829 goto qf_init_end; 830 } 831 832 if (newlist || qi->qf_curlist == qi->qf_listcount) 833 /* make place for a new list */ 834 qf_new_list(qi, qf_title); 835 #ifdef FEAT_WINDOWS 836 else if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 837 { 838 /* Adding to existing list, use last entry. */ 839 old_last = qi->qf_lists[qi->qf_curlist].qf_last; 840 } 841 #endif 842 843 /* 844 * Each part of the format string is copied and modified from errorformat to 845 * regex prog. Only a few % characters are allowed. 846 */ 847 /* Use the local value of 'errorformat' if it's set. */ 848 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL) 849 efm = buf->b_p_efm; 850 else 851 efm = errorformat; 852 853 /* 854 * If we are not adding or adding to another list: clear the state. 855 */ 856 if (newlist || qi->qf_curlist != qi->qf_dir_curlist) 857 { 858 qi->qf_dir_curlist = qi->qf_curlist; 859 qf_clean_dir_stack(&qi->qf_dir_stack); 860 qi->qf_directory = NULL; 861 qf_clean_dir_stack(&qi->qf_file_stack); 862 qi->qf_currfile = NULL; 863 qi->qf_multiline = FALSE; 864 qi->qf_multiignore = FALSE; 865 qi->qf_multiscan = FALSE; 866 } 867 868 /* 869 * If the errorformat didn't change between calls, then reuse the 870 * previously parsed values. 871 */ 872 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0)) 873 { 874 /* free the previously parsed data */ 875 vim_free(last_efm); 876 last_efm = NULL; 877 free_efm_list(&fmt_first); 878 879 /* parse the current 'efm' */ 880 fmt_first = parse_efm_option(efm); 881 if (fmt_first != NULL) 882 last_efm = vim_strsave(efm); 883 } 884 885 if (fmt_first == NULL) /* nothing found */ 886 goto error2; 887 888 /* 889 * got_int is reset here, because it was probably set when killing the 890 * ":make" command, but we still want to read the errorfile then. 891 */ 892 got_int = FALSE; 893 894 /* Always ignore case when looking for a matching error. */ 895 regmatch.rm_ic = TRUE; 896 897 if (tv != NULL) 898 { 899 if (tv->v_type == VAR_STRING) 900 state.p_str = tv->vval.v_string; 901 else if (tv->v_type == VAR_LIST) 902 state.p_li = tv->vval.v_list->lv_first; 903 state.tv = tv; 904 } 905 state.buf = buf; 906 state.buflnum = lnumfirst; 907 state.lnumlast = lnumlast; 908 909 /* 910 * Read the lines in the error file one by one. 911 * Try to recognize one of the error formats in each line. 912 */ 913 while (!got_int) 914 { 915 /* Get the next line from a file/buffer/list/string */ 916 status = qf_get_nextline(&state); 917 if (status == QF_NOMEM) /* memory alloc failure */ 918 goto qf_init_end; 919 if (status == QF_END_OF_INPUT) /* end of input */ 920 break; 921 922 /* If there was no %> item start at the first pattern */ 923 if (fmt_start == NULL) 924 fmt_ptr = fmt_first; 925 else 926 { 927 fmt_ptr = fmt_start; 928 fmt_start = NULL; 929 } 930 931 /* 932 * Try to match each part of 'errorformat' until we find a complete 933 * match or no match. 934 */ 935 valid = TRUE; 936 restofline: 937 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next) 938 { 939 int r; 940 941 idx = fmt_ptr->prefix; 942 if (qi->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL) 943 continue; 944 namebuf[0] = NUL; 945 pattern[0] = NUL; 946 if (!qi->qf_multiscan) 947 errmsg[0] = NUL; 948 lnum = 0; 949 col = 0; 950 use_viscol = FALSE; 951 enr = -1; 952 type = 0; 953 tail = NULL; 954 955 regmatch.regprog = fmt_ptr->prog; 956 r = vim_regexec(®match, state.linebuf, (colnr_T)0); 957 fmt_ptr->prog = regmatch.regprog; 958 if (r) 959 { 960 if ((idx == 'C' || idx == 'Z') && !qi->qf_multiline) 961 continue; 962 if (vim_strchr((char_u *)"EWI", idx) != NULL) 963 type = idx; 964 else 965 type = 0; 966 /* 967 * Extract error message data from matched line. 968 * We check for an actual submatch, because "\[" and "\]" in 969 * the 'errorformat' may cause the wrong submatch to be used. 970 */ 971 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */ 972 { 973 int c; 974 975 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 976 continue; 977 978 /* Expand ~/file and $HOME/file to full path. */ 979 c = *regmatch.endp[i]; 980 *regmatch.endp[i] = NUL; 981 expand_env(regmatch.startp[i], namebuf, CMDBUFFSIZE); 982 *regmatch.endp[i] = c; 983 984 if (vim_strchr((char_u *)"OPQ", idx) != NULL 985 && mch_getperm(namebuf) == -1) 986 continue; 987 } 988 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */ 989 { 990 if (regmatch.startp[i] == NULL) 991 continue; 992 enr = (int)atol((char *)regmatch.startp[i]); 993 } 994 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */ 995 { 996 if (regmatch.startp[i] == NULL) 997 continue; 998 lnum = atol((char *)regmatch.startp[i]); 999 } 1000 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */ 1001 { 1002 if (regmatch.startp[i] == NULL) 1003 continue; 1004 col = (int)atol((char *)regmatch.startp[i]); 1005 } 1006 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */ 1007 { 1008 if (regmatch.startp[i] == NULL) 1009 continue; 1010 type = *regmatch.startp[i]; 1011 } 1012 if (fmt_ptr->flags == '+' && !qi->qf_multiscan) /* %+ */ 1013 { 1014 if (state.linelen > errmsglen) { 1015 /* linelen + null terminator */ 1016 if ((errmsg = vim_realloc(errmsg, 1017 state.linelen + 1)) == NULL) 1018 goto qf_init_end; 1019 errmsglen = state.linelen + 1; 1020 } 1021 vim_strncpy(errmsg, state.linebuf, state.linelen); 1022 } 1023 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */ 1024 { 1025 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 1026 continue; 1027 len = (int)(regmatch.endp[i] - regmatch.startp[i]); 1028 if (len > errmsglen) { 1029 /* len + null terminator */ 1030 if ((errmsg = vim_realloc(errmsg, len + 1)) 1031 == NULL) 1032 goto qf_init_end; 1033 errmsglen = len + 1; 1034 } 1035 vim_strncpy(errmsg, regmatch.startp[i], len); 1036 } 1037 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */ 1038 { 1039 if (regmatch.startp[i] == NULL) 1040 continue; 1041 tail = regmatch.startp[i]; 1042 } 1043 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */ 1044 { 1045 char_u *match_ptr; 1046 1047 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 1048 continue; 1049 col = 0; 1050 for (match_ptr = regmatch.startp[i]; 1051 match_ptr != regmatch.endp[i]; ++match_ptr) 1052 { 1053 ++col; 1054 if (*match_ptr == TAB) 1055 { 1056 col += 7; 1057 col -= col % 8; 1058 } 1059 } 1060 ++col; 1061 use_viscol = TRUE; 1062 } 1063 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */ 1064 { 1065 if (regmatch.startp[i] == NULL) 1066 continue; 1067 col = (int)atol((char *)regmatch.startp[i]); 1068 use_viscol = TRUE; 1069 } 1070 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */ 1071 { 1072 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 1073 continue; 1074 len = (int)(regmatch.endp[i] - regmatch.startp[i]); 1075 if (len > CMDBUFFSIZE - 5) 1076 len = CMDBUFFSIZE - 5; 1077 STRCPY(pattern, "^\\V"); 1078 STRNCAT(pattern, regmatch.startp[i], len); 1079 pattern[len + 3] = '\\'; 1080 pattern[len + 4] = '$'; 1081 pattern[len + 5] = NUL; 1082 } 1083 break; 1084 } 1085 } 1086 qi->qf_multiscan = FALSE; 1087 1088 if (fmt_ptr == NULL || idx == 'D' || idx == 'X') 1089 { 1090 if (fmt_ptr != NULL) 1091 { 1092 if (idx == 'D') /* enter directory */ 1093 { 1094 if (*namebuf == NUL) 1095 { 1096 EMSG(_("E379: Missing or empty directory name")); 1097 goto error2; 1098 } 1099 qi->qf_directory = 1100 qf_push_dir(namebuf, &qi->qf_dir_stack, FALSE); 1101 if (qi->qf_directory == NULL) 1102 goto error2; 1103 } 1104 else if (idx == 'X') /* leave directory */ 1105 qi->qf_directory = qf_pop_dir(&qi->qf_dir_stack); 1106 } 1107 namebuf[0] = NUL; /* no match found, remove file name */ 1108 lnum = 0; /* don't jump to this line */ 1109 valid = FALSE; 1110 if (state.linelen > errmsglen) { 1111 /* linelen + null terminator */ 1112 if ((errmsg = vim_realloc(errmsg, state.linelen + 1)) == NULL) 1113 goto qf_init_end; 1114 errmsglen = state.linelen + 1; 1115 } 1116 /* copy whole line to error message */ 1117 vim_strncpy(errmsg, state.linebuf, state.linelen); 1118 if (fmt_ptr == NULL) 1119 qi->qf_multiline = qi->qf_multiignore = FALSE; 1120 } 1121 else if (fmt_ptr != NULL) 1122 { 1123 /* honor %> item */ 1124 if (fmt_ptr->conthere) 1125 fmt_start = fmt_ptr; 1126 1127 if (vim_strchr((char_u *)"AEWI", idx) != NULL) 1128 { 1129 qi->qf_multiline = TRUE; /* start of a multi-line message */ 1130 qi->qf_multiignore = FALSE; /* reset continuation */ 1131 } 1132 else if (vim_strchr((char_u *)"CZ", idx) != NULL) 1133 { /* continuation of multi-line msg */ 1134 qfline_T *qfprev = qi->qf_lists[qi->qf_curlist].qf_last; 1135 1136 if (qfprev == NULL) 1137 goto error2; 1138 if (*errmsg && !qi->qf_multiignore) 1139 { 1140 len = (int)STRLEN(qfprev->qf_text); 1141 if ((ptr = alloc((unsigned)(len + STRLEN(errmsg) + 2))) 1142 == NULL) 1143 goto error2; 1144 STRCPY(ptr, qfprev->qf_text); 1145 vim_free(qfprev->qf_text); 1146 qfprev->qf_text = ptr; 1147 *(ptr += len) = '\n'; 1148 STRCPY(++ptr, errmsg); 1149 } 1150 if (qfprev->qf_nr == -1) 1151 qfprev->qf_nr = enr; 1152 if (vim_isprintc(type) && !qfprev->qf_type) 1153 qfprev->qf_type = type; /* only printable chars allowed */ 1154 if (!qfprev->qf_lnum) 1155 qfprev->qf_lnum = lnum; 1156 if (!qfprev->qf_col) 1157 qfprev->qf_col = col; 1158 qfprev->qf_viscol = use_viscol; 1159 if (!qfprev->qf_fnum) 1160 qfprev->qf_fnum = qf_get_fnum(qi, qi->qf_directory, 1161 *namebuf || qi->qf_directory != NULL 1162 ? namebuf 1163 : qi->qf_currfile != NULL && valid 1164 ? qi->qf_currfile : 0); 1165 if (idx == 'Z') 1166 qi->qf_multiline = qi->qf_multiignore = FALSE; 1167 line_breakcheck(); 1168 continue; 1169 } 1170 else if (vim_strchr((char_u *)"OPQ", idx) != NULL) 1171 { 1172 /* global file names */ 1173 valid = FALSE; 1174 if (*namebuf == NUL || mch_getperm(namebuf) >= 0) 1175 { 1176 if (*namebuf && idx == 'P') 1177 qi->qf_currfile = 1178 qf_push_dir(namebuf, &qi->qf_file_stack, TRUE); 1179 else if (idx == 'Q') 1180 qi->qf_currfile = qf_pop_dir(&qi->qf_file_stack); 1181 *namebuf = NUL; 1182 if (tail && *tail) 1183 { 1184 STRMOVE(IObuff, skipwhite(tail)); 1185 qi->qf_multiscan = TRUE; 1186 goto restofline; 1187 } 1188 } 1189 } 1190 if (fmt_ptr->flags == '-') /* generally exclude this line */ 1191 { 1192 if (qi->qf_multiline) 1193 /* also exclude continuation lines */ 1194 qi->qf_multiignore = TRUE; 1195 continue; 1196 } 1197 } 1198 1199 if (qf_add_entry(qi, 1200 qi->qf_directory, 1201 (*namebuf || qi->qf_directory != NULL) 1202 ? namebuf 1203 : ((qi->qf_currfile != NULL && valid) 1204 ? qi->qf_currfile : (char_u *)NULL), 1205 0, 1206 errmsg, 1207 lnum, 1208 col, 1209 use_viscol, 1210 pattern, 1211 enr, 1212 type, 1213 valid) == FAIL) 1214 goto error2; 1215 line_breakcheck(); 1216 } 1217 if (state.fd == NULL || !ferror(state.fd)) 1218 { 1219 if (qi->qf_lists[qi->qf_curlist].qf_index == 0) 1220 { 1221 /* no valid entry found */ 1222 qi->qf_lists[qi->qf_curlist].qf_ptr = 1223 qi->qf_lists[qi->qf_curlist].qf_start; 1224 qi->qf_lists[qi->qf_curlist].qf_index = 1; 1225 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE; 1226 } 1227 else 1228 { 1229 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 1230 if (qi->qf_lists[qi->qf_curlist].qf_ptr == NULL) 1231 qi->qf_lists[qi->qf_curlist].qf_ptr = 1232 qi->qf_lists[qi->qf_curlist].qf_start; 1233 } 1234 /* return number of matches */ 1235 retval = qi->qf_lists[qi->qf_curlist].qf_count; 1236 goto qf_init_end; 1237 } 1238 EMSG(_(e_readerrf)); 1239 error2: 1240 qf_free(qi, qi->qf_curlist); 1241 qi->qf_listcount--; 1242 if (qi->qf_curlist > 0) 1243 --qi->qf_curlist; 1244 qf_init_end: 1245 if (state.fd != NULL) 1246 fclose(state.fd); 1247 vim_free(namebuf); 1248 vim_free(errmsg); 1249 vim_free(pattern); 1250 vim_free(state.growbuf); 1251 1252 #ifdef FEAT_WINDOWS 1253 qf_update_buffer(qi, old_last); 1254 #endif 1255 1256 return retval; 1257 } 1258 1259 static void 1260 qf_store_title(qf_info_T *qi, char_u *title) 1261 { 1262 if (title != NULL) 1263 { 1264 char_u *p = alloc((int)STRLEN(title) + 2); 1265 1266 qi->qf_lists[qi->qf_curlist].qf_title = p; 1267 if (p != NULL) 1268 sprintf((char *)p, ":%s", (char *)title); 1269 } 1270 } 1271 1272 /* 1273 * Prepare for adding a new quickfix list. 1274 */ 1275 static void 1276 qf_new_list(qf_info_T *qi, char_u *qf_title) 1277 { 1278 int i; 1279 1280 /* 1281 * If the current entry is not the last entry, delete entries beyond 1282 * the current entry. This makes it possible to browse in a tree-like 1283 * way with ":grep'. 1284 */ 1285 while (qi->qf_listcount > qi->qf_curlist + 1) 1286 qf_free(qi, --qi->qf_listcount); 1287 1288 /* 1289 * When the stack is full, remove to oldest entry 1290 * Otherwise, add a new entry. 1291 */ 1292 if (qi->qf_listcount == LISTCOUNT) 1293 { 1294 qf_free(qi, 0); 1295 for (i = 1; i < LISTCOUNT; ++i) 1296 qi->qf_lists[i - 1] = qi->qf_lists[i]; 1297 qi->qf_curlist = LISTCOUNT - 1; 1298 } 1299 else 1300 qi->qf_curlist = qi->qf_listcount++; 1301 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T))); 1302 qf_store_title(qi, qf_title); 1303 } 1304 1305 /* 1306 * Free a location list 1307 */ 1308 static void 1309 ll_free_all(qf_info_T **pqi) 1310 { 1311 int i; 1312 qf_info_T *qi; 1313 1314 qi = *pqi; 1315 if (qi == NULL) 1316 return; 1317 *pqi = NULL; /* Remove reference to this list */ 1318 1319 qi->qf_refcount--; 1320 if (qi->qf_refcount < 1) 1321 { 1322 /* No references to this location list */ 1323 for (i = 0; i < qi->qf_listcount; ++i) 1324 qf_free(qi, i); 1325 vim_free(qi); 1326 } 1327 } 1328 1329 void 1330 qf_free_all(win_T *wp) 1331 { 1332 int i; 1333 qf_info_T *qi = &ql_info; 1334 1335 if (wp != NULL) 1336 { 1337 /* location list */ 1338 ll_free_all(&wp->w_llist); 1339 ll_free_all(&wp->w_llist_ref); 1340 } 1341 else 1342 /* quickfix list */ 1343 for (i = 0; i < qi->qf_listcount; ++i) 1344 qf_free(qi, i); 1345 } 1346 1347 /* 1348 * Add an entry to the end of the list of errors. 1349 * Returns OK or FAIL. 1350 */ 1351 static int 1352 qf_add_entry( 1353 qf_info_T *qi, /* quickfix list */ 1354 char_u *dir, /* optional directory name */ 1355 char_u *fname, /* file name or NULL */ 1356 int bufnum, /* buffer number or zero */ 1357 char_u *mesg, /* message */ 1358 long lnum, /* line number */ 1359 int col, /* column */ 1360 int vis_col, /* using visual column */ 1361 char_u *pattern, /* search pattern */ 1362 int nr, /* error number */ 1363 int type, /* type character */ 1364 int valid) /* valid entry */ 1365 { 1366 qfline_T *qfp; 1367 qfline_T **lastp; /* pointer to qf_last or NULL */ 1368 1369 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL) 1370 return FAIL; 1371 if (bufnum != 0) 1372 { 1373 buf_T *buf = buflist_findnr(bufnum); 1374 1375 qfp->qf_fnum = bufnum; 1376 if (buf != NULL) 1377 buf->b_has_qf_entry = TRUE; 1378 } 1379 else 1380 qfp->qf_fnum = qf_get_fnum(qi, dir, fname); 1381 if ((qfp->qf_text = vim_strsave(mesg)) == NULL) 1382 { 1383 vim_free(qfp); 1384 return FAIL; 1385 } 1386 qfp->qf_lnum = lnum; 1387 qfp->qf_col = col; 1388 qfp->qf_viscol = vis_col; 1389 if (pattern == NULL || *pattern == NUL) 1390 qfp->qf_pattern = NULL; 1391 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL) 1392 { 1393 vim_free(qfp->qf_text); 1394 vim_free(qfp); 1395 return FAIL; 1396 } 1397 qfp->qf_nr = nr; 1398 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */ 1399 type = 0; 1400 qfp->qf_type = type; 1401 qfp->qf_valid = valid; 1402 1403 lastp = &qi->qf_lists[qi->qf_curlist].qf_last; 1404 if (qi->qf_lists[qi->qf_curlist].qf_count == 0) 1405 /* first element in the list */ 1406 { 1407 qi->qf_lists[qi->qf_curlist].qf_start = qfp; 1408 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp; 1409 qi->qf_lists[qi->qf_curlist].qf_index = 0; 1410 qfp->qf_prev = NULL; 1411 } 1412 else 1413 { 1414 qfp->qf_prev = *lastp; 1415 (*lastp)->qf_next = qfp; 1416 } 1417 qfp->qf_next = NULL; 1418 qfp->qf_cleared = FALSE; 1419 *lastp = qfp; 1420 ++qi->qf_lists[qi->qf_curlist].qf_count; 1421 if (qi->qf_lists[qi->qf_curlist].qf_index == 0 && qfp->qf_valid) 1422 /* first valid entry */ 1423 { 1424 qi->qf_lists[qi->qf_curlist].qf_index = 1425 qi->qf_lists[qi->qf_curlist].qf_count; 1426 qi->qf_lists[qi->qf_curlist].qf_ptr = qfp; 1427 } 1428 1429 return OK; 1430 } 1431 1432 /* 1433 * Allocate a new location list 1434 */ 1435 static qf_info_T * 1436 ll_new_list(void) 1437 { 1438 qf_info_T *qi; 1439 1440 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T)); 1441 if (qi != NULL) 1442 { 1443 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T))); 1444 qi->qf_refcount++; 1445 } 1446 1447 return qi; 1448 } 1449 1450 /* 1451 * Return the location list for window 'wp'. 1452 * If not present, allocate a location list 1453 */ 1454 static qf_info_T * 1455 ll_get_or_alloc_list(win_T *wp) 1456 { 1457 if (IS_LL_WINDOW(wp)) 1458 /* For a location list window, use the referenced location list */ 1459 return wp->w_llist_ref; 1460 1461 /* 1462 * For a non-location list window, w_llist_ref should not point to a 1463 * location list. 1464 */ 1465 ll_free_all(&wp->w_llist_ref); 1466 1467 if (wp->w_llist == NULL) 1468 wp->w_llist = ll_new_list(); /* new location list */ 1469 return wp->w_llist; 1470 } 1471 1472 /* 1473 * Copy the location list from window "from" to window "to". 1474 */ 1475 void 1476 copy_loclist(win_T *from, win_T *to) 1477 { 1478 qf_info_T *qi; 1479 int idx; 1480 int i; 1481 1482 /* 1483 * When copying from a location list window, copy the referenced 1484 * location list. For other windows, copy the location list for 1485 * that window. 1486 */ 1487 if (IS_LL_WINDOW(from)) 1488 qi = from->w_llist_ref; 1489 else 1490 qi = from->w_llist; 1491 1492 if (qi == NULL) /* no location list to copy */ 1493 return; 1494 1495 /* allocate a new location list */ 1496 if ((to->w_llist = ll_new_list()) == NULL) 1497 return; 1498 1499 to->w_llist->qf_listcount = qi->qf_listcount; 1500 1501 /* Copy the location lists one at a time */ 1502 for (idx = 0; idx < qi->qf_listcount; idx++) 1503 { 1504 qf_list_T *from_qfl; 1505 qf_list_T *to_qfl; 1506 1507 to->w_llist->qf_curlist = idx; 1508 1509 from_qfl = &qi->qf_lists[idx]; 1510 to_qfl = &to->w_llist->qf_lists[idx]; 1511 1512 /* Some of the fields are populated by qf_add_entry() */ 1513 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid; 1514 to_qfl->qf_count = 0; 1515 to_qfl->qf_index = 0; 1516 to_qfl->qf_start = NULL; 1517 to_qfl->qf_last = NULL; 1518 to_qfl->qf_ptr = NULL; 1519 if (from_qfl->qf_title != NULL) 1520 to_qfl->qf_title = vim_strsave(from_qfl->qf_title); 1521 else 1522 to_qfl->qf_title = NULL; 1523 1524 if (from_qfl->qf_count) 1525 { 1526 qfline_T *from_qfp; 1527 qfline_T *prevp; 1528 1529 /* copy all the location entries in this list */ 1530 for (i = 0, from_qfp = from_qfl->qf_start; 1531 i < from_qfl->qf_count && from_qfp != NULL; 1532 ++i, from_qfp = from_qfp->qf_next) 1533 { 1534 if (qf_add_entry(to->w_llist, 1535 NULL, 1536 NULL, 1537 0, 1538 from_qfp->qf_text, 1539 from_qfp->qf_lnum, 1540 from_qfp->qf_col, 1541 from_qfp->qf_viscol, 1542 from_qfp->qf_pattern, 1543 from_qfp->qf_nr, 1544 0, 1545 from_qfp->qf_valid) == FAIL) 1546 { 1547 qf_free_all(to); 1548 return; 1549 } 1550 /* 1551 * qf_add_entry() will not set the qf_num field, as the 1552 * directory and file names are not supplied. So the qf_fnum 1553 * field is copied here. 1554 */ 1555 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last; 1556 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */ 1557 prevp->qf_type = from_qfp->qf_type; /* error type */ 1558 if (from_qfl->qf_ptr == from_qfp) 1559 to_qfl->qf_ptr = prevp; /* current location */ 1560 } 1561 } 1562 1563 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */ 1564 1565 /* When no valid entries are present in the list, qf_ptr points to 1566 * the first item in the list */ 1567 if (to_qfl->qf_nonevalid) 1568 { 1569 to_qfl->qf_ptr = to_qfl->qf_start; 1570 to_qfl->qf_index = 1; 1571 } 1572 } 1573 1574 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */ 1575 } 1576 1577 /* 1578 * Looking up a buffer can be slow if there are many. Remember the last one 1579 * to make this a lot faster if there are multiple matches in the same file. 1580 */ 1581 static char_u *qf_last_bufname = NULL; 1582 static bufref_T qf_last_bufref = {NULL, 0}; 1583 1584 /* 1585 * Get buffer number for file "dir.name". 1586 * Also sets the b_has_qf_entry flag. 1587 */ 1588 static int 1589 qf_get_fnum(qf_info_T *qi, char_u *directory, char_u *fname) 1590 { 1591 char_u *ptr = NULL; 1592 buf_T *buf; 1593 char_u *bufname; 1594 1595 if (fname == NULL || *fname == NUL) /* no file name */ 1596 return 0; 1597 1598 #ifdef VMS 1599 vms_remove_version(fname); 1600 #endif 1601 #ifdef BACKSLASH_IN_FILENAME 1602 if (directory != NULL) 1603 slash_adjust(directory); 1604 slash_adjust(fname); 1605 #endif 1606 if (directory != NULL && !vim_isAbsName(fname) 1607 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL) 1608 { 1609 /* 1610 * Here we check if the file really exists. 1611 * This should normally be true, but if make works without 1612 * "leaving directory"-messages we might have missed a 1613 * directory change. 1614 */ 1615 if (mch_getperm(ptr) < 0) 1616 { 1617 vim_free(ptr); 1618 directory = qf_guess_filepath(qi, fname); 1619 if (directory) 1620 ptr = concat_fnames(directory, fname, TRUE); 1621 else 1622 ptr = vim_strsave(fname); 1623 } 1624 /* Use concatenated directory name and file name */ 1625 bufname = ptr; 1626 } 1627 else 1628 bufname = fname; 1629 1630 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0 1631 && bufref_valid(&qf_last_bufref)) 1632 { 1633 buf = qf_last_bufref.br_buf; 1634 vim_free(ptr); 1635 } 1636 else 1637 { 1638 vim_free(qf_last_bufname); 1639 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT); 1640 if (bufname == ptr) 1641 qf_last_bufname = bufname; 1642 else 1643 qf_last_bufname = vim_strsave(bufname); 1644 set_bufref(&qf_last_bufref, buf); 1645 } 1646 if (buf == NULL) 1647 return 0; 1648 1649 buf->b_has_qf_entry = TRUE; 1650 return buf->b_fnum; 1651 } 1652 1653 /* 1654 * Push dirbuf onto the directory stack and return pointer to actual dir or 1655 * NULL on error. 1656 */ 1657 static char_u * 1658 qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack) 1659 { 1660 struct dir_stack_T *ds_new; 1661 struct dir_stack_T *ds_ptr; 1662 1663 /* allocate new stack element and hook it in */ 1664 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T)); 1665 if (ds_new == NULL) 1666 return NULL; 1667 1668 ds_new->next = *stackptr; 1669 *stackptr = ds_new; 1670 1671 /* store directory on the stack */ 1672 if (vim_isAbsName(dirbuf) 1673 || (*stackptr)->next == NULL 1674 || (*stackptr && is_file_stack)) 1675 (*stackptr)->dirname = vim_strsave(dirbuf); 1676 else 1677 { 1678 /* Okay we don't have an absolute path. 1679 * dirbuf must be a subdir of one of the directories on the stack. 1680 * Let's search... 1681 */ 1682 ds_new = (*stackptr)->next; 1683 (*stackptr)->dirname = NULL; 1684 while (ds_new) 1685 { 1686 vim_free((*stackptr)->dirname); 1687 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf, 1688 TRUE); 1689 if (mch_isdir((*stackptr)->dirname) == TRUE) 1690 break; 1691 1692 ds_new = ds_new->next; 1693 } 1694 1695 /* clean up all dirs we already left */ 1696 while ((*stackptr)->next != ds_new) 1697 { 1698 ds_ptr = (*stackptr)->next; 1699 (*stackptr)->next = (*stackptr)->next->next; 1700 vim_free(ds_ptr->dirname); 1701 vim_free(ds_ptr); 1702 } 1703 1704 /* Nothing found -> it must be on top level */ 1705 if (ds_new == NULL) 1706 { 1707 vim_free((*stackptr)->dirname); 1708 (*stackptr)->dirname = vim_strsave(dirbuf); 1709 } 1710 } 1711 1712 if ((*stackptr)->dirname != NULL) 1713 return (*stackptr)->dirname; 1714 else 1715 { 1716 ds_ptr = *stackptr; 1717 *stackptr = (*stackptr)->next; 1718 vim_free(ds_ptr); 1719 return NULL; 1720 } 1721 } 1722 1723 1724 /* 1725 * pop dirbuf from the directory stack and return previous directory or NULL if 1726 * stack is empty 1727 */ 1728 static char_u * 1729 qf_pop_dir(struct dir_stack_T **stackptr) 1730 { 1731 struct dir_stack_T *ds_ptr; 1732 1733 /* TODO: Should we check if dirbuf is the directory on top of the stack? 1734 * What to do if it isn't? */ 1735 1736 /* pop top element and free it */ 1737 if (*stackptr != NULL) 1738 { 1739 ds_ptr = *stackptr; 1740 *stackptr = (*stackptr)->next; 1741 vim_free(ds_ptr->dirname); 1742 vim_free(ds_ptr); 1743 } 1744 1745 /* return NEW top element as current dir or NULL if stack is empty*/ 1746 return *stackptr ? (*stackptr)->dirname : NULL; 1747 } 1748 1749 /* 1750 * clean up directory stack 1751 */ 1752 static void 1753 qf_clean_dir_stack(struct dir_stack_T **stackptr) 1754 { 1755 struct dir_stack_T *ds_ptr; 1756 1757 while ((ds_ptr = *stackptr) != NULL) 1758 { 1759 *stackptr = (*stackptr)->next; 1760 vim_free(ds_ptr->dirname); 1761 vim_free(ds_ptr); 1762 } 1763 } 1764 1765 /* 1766 * Check in which directory of the directory stack the given file can be 1767 * found. 1768 * Returns a pointer to the directory name or NULL if not found. 1769 * Cleans up intermediate directory entries. 1770 * 1771 * TODO: How to solve the following problem? 1772 * If we have the this directory tree: 1773 * ./ 1774 * ./aa 1775 * ./aa/bb 1776 * ./bb 1777 * ./bb/x.c 1778 * and make says: 1779 * making all in aa 1780 * making all in bb 1781 * x.c:9: Error 1782 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb. 1783 * qf_guess_filepath will return NULL. 1784 */ 1785 static char_u * 1786 qf_guess_filepath(qf_info_T *qi, char_u *filename) 1787 { 1788 struct dir_stack_T *ds_ptr; 1789 struct dir_stack_T *ds_tmp; 1790 char_u *fullname; 1791 1792 /* no dirs on the stack - there's nothing we can do */ 1793 if (qi->qf_dir_stack == NULL) 1794 return NULL; 1795 1796 ds_ptr = qi->qf_dir_stack->next; 1797 fullname = NULL; 1798 while (ds_ptr) 1799 { 1800 vim_free(fullname); 1801 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE); 1802 1803 /* If concat_fnames failed, just go on. The worst thing that can happen 1804 * is that we delete the entire stack. 1805 */ 1806 if ((fullname != NULL) && (mch_getperm(fullname) >= 0)) 1807 break; 1808 1809 ds_ptr = ds_ptr->next; 1810 } 1811 1812 vim_free(fullname); 1813 1814 /* clean up all dirs we already left */ 1815 while (qi->qf_dir_stack->next != ds_ptr) 1816 { 1817 ds_tmp = qi->qf_dir_stack->next; 1818 qi->qf_dir_stack->next = qi->qf_dir_stack->next->next; 1819 vim_free(ds_tmp->dirname); 1820 vim_free(ds_tmp); 1821 } 1822 1823 return ds_ptr==NULL? NULL: ds_ptr->dirname; 1824 } 1825 1826 /* 1827 * When loading a file from the quickfix, the auto commands may modify it. 1828 * This may invalidate the current quickfix entry. This function checks 1829 * whether a entry is still present in the quickfix. 1830 * Similar to location list. 1831 */ 1832 static int 1833 is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr) 1834 { 1835 qf_list_T *qfl; 1836 qfline_T *qfp; 1837 int i; 1838 1839 qfl = &qi->qf_lists[qi->qf_curlist]; 1840 1841 /* Search for the entry in the current list */ 1842 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count; 1843 ++i, qfp = qfp->qf_next) 1844 if (qfp == NULL || qfp == qf_ptr) 1845 break; 1846 1847 if (i == qfl->qf_count) /* Entry is not found */ 1848 return FALSE; 1849 1850 return TRUE; 1851 } 1852 1853 /* 1854 * jump to a quickfix line 1855 * if dir == FORWARD go "errornr" valid entries forward 1856 * if dir == BACKWARD go "errornr" valid entries backward 1857 * if dir == FORWARD_FILE go "errornr" valid entries files backward 1858 * if dir == BACKWARD_FILE go "errornr" valid entries files backward 1859 * else if "errornr" is zero, redisplay the same line 1860 * else go to entry "errornr" 1861 */ 1862 void 1863 qf_jump( 1864 qf_info_T *qi, 1865 int dir, 1866 int errornr, 1867 int forceit) 1868 { 1869 qf_info_T *ll_ref; 1870 qfline_T *qf_ptr; 1871 qfline_T *old_qf_ptr; 1872 int qf_index; 1873 int old_qf_fnum; 1874 int old_qf_index; 1875 int prev_index; 1876 static char_u *e_no_more_items = (char_u *)N_("E553: No more items"); 1877 char_u *err = e_no_more_items; 1878 linenr_T i; 1879 buf_T *old_curbuf; 1880 linenr_T old_lnum; 1881 colnr_T screen_col; 1882 colnr_T char_col; 1883 char_u *line; 1884 #ifdef FEAT_WINDOWS 1885 char_u *old_swb = p_swb; 1886 unsigned old_swb_flags = swb_flags; 1887 int opened_window = FALSE; 1888 win_T *win; 1889 win_T *altwin; 1890 int flags; 1891 #endif 1892 win_T *oldwin = curwin; 1893 int print_message = TRUE; 1894 int len; 1895 #ifdef FEAT_FOLDING 1896 int old_KeyTyped = KeyTyped; /* getting file may reset it */ 1897 #endif 1898 int ok = OK; 1899 int usable_win; 1900 1901 if (qi == NULL) 1902 qi = &ql_info; 1903 1904 if (qi->qf_curlist >= qi->qf_listcount 1905 || qi->qf_lists[qi->qf_curlist].qf_count == 0) 1906 { 1907 EMSG(_(e_quickfix)); 1908 return; 1909 } 1910 1911 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr; 1912 old_qf_ptr = qf_ptr; 1913 qf_index = qi->qf_lists[qi->qf_curlist].qf_index; 1914 old_qf_index = qf_index; 1915 if (dir == FORWARD || dir == FORWARD_FILE) /* next valid entry */ 1916 { 1917 while (errornr--) 1918 { 1919 old_qf_ptr = qf_ptr; 1920 prev_index = qf_index; 1921 old_qf_fnum = qf_ptr->qf_fnum; 1922 do 1923 { 1924 if (qf_index == qi->qf_lists[qi->qf_curlist].qf_count 1925 || qf_ptr->qf_next == NULL) 1926 { 1927 qf_ptr = old_qf_ptr; 1928 qf_index = prev_index; 1929 if (err != NULL) 1930 { 1931 EMSG(_(err)); 1932 goto theend; 1933 } 1934 errornr = 0; 1935 break; 1936 } 1937 ++qf_index; 1938 qf_ptr = qf_ptr->qf_next; 1939 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid 1940 && !qf_ptr->qf_valid) 1941 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); 1942 err = NULL; 1943 } 1944 } 1945 else if (dir == BACKWARD || dir == BACKWARD_FILE) /* prev. valid entry */ 1946 { 1947 while (errornr--) 1948 { 1949 old_qf_ptr = qf_ptr; 1950 prev_index = qf_index; 1951 old_qf_fnum = qf_ptr->qf_fnum; 1952 do 1953 { 1954 if (qf_index == 1 || qf_ptr->qf_prev == NULL) 1955 { 1956 qf_ptr = old_qf_ptr; 1957 qf_index = prev_index; 1958 if (err != NULL) 1959 { 1960 EMSG(_(err)); 1961 goto theend; 1962 } 1963 errornr = 0; 1964 break; 1965 } 1966 --qf_index; 1967 qf_ptr = qf_ptr->qf_prev; 1968 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid 1969 && !qf_ptr->qf_valid) 1970 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); 1971 err = NULL; 1972 } 1973 } 1974 else if (errornr != 0) /* go to specified number */ 1975 { 1976 while (errornr < qf_index && qf_index > 1 && qf_ptr->qf_prev != NULL) 1977 { 1978 --qf_index; 1979 qf_ptr = qf_ptr->qf_prev; 1980 } 1981 while (errornr > qf_index && qf_index < 1982 qi->qf_lists[qi->qf_curlist].qf_count 1983 && qf_ptr->qf_next != NULL) 1984 { 1985 ++qf_index; 1986 qf_ptr = qf_ptr->qf_next; 1987 } 1988 } 1989 1990 #ifdef FEAT_WINDOWS 1991 qi->qf_lists[qi->qf_curlist].qf_index = qf_index; 1992 if (qf_win_pos_update(qi, old_qf_index)) 1993 /* No need to print the error message if it's visible in the error 1994 * window */ 1995 print_message = FALSE; 1996 1997 /* 1998 * For ":helpgrep" find a help window or open one. 1999 */ 2000 if (qf_ptr->qf_type == 1 && (!curwin->w_buffer->b_help || cmdmod.tab != 0)) 2001 { 2002 win_T *wp; 2003 2004 if (cmdmod.tab != 0) 2005 wp = NULL; 2006 else 2007 for (wp = firstwin; wp != NULL; wp = wp->w_next) 2008 if (wp->w_buffer != NULL && wp->w_buffer->b_help) 2009 break; 2010 if (wp != NULL && wp->w_buffer->b_nwindows > 0) 2011 win_enter(wp, TRUE); 2012 else 2013 { 2014 /* 2015 * Split off help window; put it at far top if no position 2016 * specified, the current window is vertically split and narrow. 2017 */ 2018 flags = WSP_HELP; 2019 if (cmdmod.split == 0 && curwin->w_width != Columns 2020 && curwin->w_width < 80) 2021 flags |= WSP_TOP; 2022 if (qi != &ql_info) 2023 flags |= WSP_NEWLOC; /* don't copy the location list */ 2024 2025 if (win_split(0, flags) == FAIL) 2026 goto theend; 2027 opened_window = TRUE; /* close it when fail */ 2028 2029 if (curwin->w_height < p_hh) 2030 win_setheight((int)p_hh); 2031 2032 if (qi != &ql_info) /* not a quickfix list */ 2033 { 2034 /* The new window should use the supplied location list */ 2035 curwin->w_llist = qi; 2036 qi->qf_refcount++; 2037 } 2038 } 2039 2040 if (!p_im) 2041 restart_edit = 0; /* don't want insert mode in help file */ 2042 } 2043 2044 /* 2045 * If currently in the quickfix window, find another window to show the 2046 * file in. 2047 */ 2048 if (bt_quickfix(curbuf) && !opened_window) 2049 { 2050 win_T *usable_win_ptr = NULL; 2051 2052 /* 2053 * If there is no file specified, we don't know where to go. 2054 * But do advance, otherwise ":cn" gets stuck. 2055 */ 2056 if (qf_ptr->qf_fnum == 0) 2057 goto theend; 2058 2059 usable_win = 0; 2060 2061 ll_ref = curwin->w_llist_ref; 2062 if (ll_ref != NULL) 2063 { 2064 /* Find a window using the same location list that is not a 2065 * quickfix window. */ 2066 FOR_ALL_WINDOWS(usable_win_ptr) 2067 if (usable_win_ptr->w_llist == ll_ref 2068 && usable_win_ptr->w_buffer->b_p_bt[0] != 'q') 2069 { 2070 usable_win = 1; 2071 break; 2072 } 2073 } 2074 2075 if (!usable_win) 2076 { 2077 /* Locate a window showing a normal buffer */ 2078 FOR_ALL_WINDOWS(win) 2079 if (win->w_buffer->b_p_bt[0] == NUL) 2080 { 2081 usable_win = 1; 2082 break; 2083 } 2084 } 2085 2086 /* 2087 * If no usable window is found and 'switchbuf' contains "usetab" 2088 * then search in other tabs. 2089 */ 2090 if (!usable_win && (swb_flags & SWB_USETAB)) 2091 { 2092 tabpage_T *tp; 2093 win_T *wp; 2094 2095 FOR_ALL_TAB_WINDOWS(tp, wp) 2096 { 2097 if (wp->w_buffer->b_fnum == qf_ptr->qf_fnum) 2098 { 2099 goto_tabpage_win(tp, wp); 2100 usable_win = 1; 2101 goto win_found; 2102 } 2103 } 2104 } 2105 win_found: 2106 2107 /* 2108 * If there is only one window and it is the quickfix window, create a 2109 * new one above the quickfix window. 2110 */ 2111 if (((firstwin == lastwin) && bt_quickfix(curbuf)) || !usable_win) 2112 { 2113 flags = WSP_ABOVE; 2114 if (ll_ref != NULL) 2115 flags |= WSP_NEWLOC; 2116 if (win_split(0, flags) == FAIL) 2117 goto failed; /* not enough room for window */ 2118 opened_window = TRUE; /* close it when fail */ 2119 p_swb = empty_option; /* don't split again */ 2120 swb_flags = 0; 2121 RESET_BINDING(curwin); 2122 if (ll_ref != NULL) 2123 { 2124 /* The new window should use the location list from the 2125 * location list window */ 2126 curwin->w_llist = ll_ref; 2127 ll_ref->qf_refcount++; 2128 } 2129 } 2130 else 2131 { 2132 if (curwin->w_llist_ref != NULL) 2133 { 2134 /* In a location window */ 2135 win = usable_win_ptr; 2136 if (win == NULL) 2137 { 2138 /* Find the window showing the selected file */ 2139 FOR_ALL_WINDOWS(win) 2140 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum) 2141 break; 2142 if (win == NULL) 2143 { 2144 /* Find a previous usable window */ 2145 win = curwin; 2146 do 2147 { 2148 if (win->w_buffer->b_p_bt[0] == NUL) 2149 break; 2150 if (win->w_prev == NULL) 2151 win = lastwin; /* wrap around the top */ 2152 else 2153 win = win->w_prev; /* go to previous window */ 2154 } while (win != curwin); 2155 } 2156 } 2157 win_goto(win); 2158 2159 /* If the location list for the window is not set, then set it 2160 * to the location list from the location window */ 2161 if (win->w_llist == NULL) 2162 { 2163 win->w_llist = ll_ref; 2164 ll_ref->qf_refcount++; 2165 } 2166 } 2167 else 2168 { 2169 2170 /* 2171 * Try to find a window that shows the right buffer. 2172 * Default to the window just above the quickfix buffer. 2173 */ 2174 win = curwin; 2175 altwin = NULL; 2176 for (;;) 2177 { 2178 if (win->w_buffer->b_fnum == qf_ptr->qf_fnum) 2179 break; 2180 if (win->w_prev == NULL) 2181 win = lastwin; /* wrap around the top */ 2182 else 2183 win = win->w_prev; /* go to previous window */ 2184 2185 if (IS_QF_WINDOW(win)) 2186 { 2187 /* Didn't find it, go to the window before the quickfix 2188 * window. */ 2189 if (altwin != NULL) 2190 win = altwin; 2191 else if (curwin->w_prev != NULL) 2192 win = curwin->w_prev; 2193 else 2194 win = curwin->w_next; 2195 break; 2196 } 2197 2198 /* Remember a usable window. */ 2199 if (altwin == NULL && !win->w_p_pvw 2200 && win->w_buffer->b_p_bt[0] == NUL) 2201 altwin = win; 2202 } 2203 2204 win_goto(win); 2205 } 2206 } 2207 } 2208 #endif 2209 2210 /* 2211 * If there is a file name, 2212 * read the wanted file if needed, and check autowrite etc. 2213 */ 2214 old_curbuf = curbuf; 2215 old_lnum = curwin->w_cursor.lnum; 2216 2217 if (qf_ptr->qf_fnum != 0) 2218 { 2219 if (qf_ptr->qf_type == 1) 2220 { 2221 /* Open help file (do_ecmd() will set b_help flag, readfile() will 2222 * set b_p_ro flag). */ 2223 if (!can_abandon(curbuf, forceit)) 2224 { 2225 EMSG(_(e_nowrtmsg)); 2226 ok = FALSE; 2227 } 2228 else 2229 ok = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1, 2230 ECMD_HIDE + ECMD_SET_HELP, 2231 oldwin == curwin ? curwin : NULL); 2232 } 2233 else 2234 { 2235 int old_qf_curlist = qi->qf_curlist; 2236 int is_abort = FALSE; 2237 2238 ok = buflist_getfile(qf_ptr->qf_fnum, 2239 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit); 2240 if (qi != &ql_info && !win_valid(oldwin)) 2241 { 2242 EMSG(_("E924: Current window was closed")); 2243 is_abort = TRUE; 2244 opened_window = FALSE; 2245 } 2246 else if (old_qf_curlist != qi->qf_curlist 2247 || !is_qf_entry_present(qi, qf_ptr)) 2248 { 2249 if (qi == &ql_info) 2250 EMSG(_("E925: Current quickfix was changed")); 2251 else 2252 EMSG(_("E926: Current location list was changed")); 2253 is_abort = TRUE; 2254 } 2255 2256 if (is_abort) 2257 { 2258 ok = FALSE; 2259 qi = NULL; 2260 qf_ptr = NULL; 2261 } 2262 } 2263 } 2264 2265 if (ok == OK) 2266 { 2267 /* When not switched to another buffer, still need to set pc mark */ 2268 if (curbuf == old_curbuf) 2269 setpcmark(); 2270 2271 if (qf_ptr->qf_pattern == NULL) 2272 { 2273 /* 2274 * Go to line with error, unless qf_lnum is 0. 2275 */ 2276 i = qf_ptr->qf_lnum; 2277 if (i > 0) 2278 { 2279 if (i > curbuf->b_ml.ml_line_count) 2280 i = curbuf->b_ml.ml_line_count; 2281 curwin->w_cursor.lnum = i; 2282 } 2283 if (qf_ptr->qf_col > 0) 2284 { 2285 curwin->w_cursor.col = qf_ptr->qf_col - 1; 2286 #ifdef FEAT_VIRTUALEDIT 2287 curwin->w_cursor.coladd = 0; 2288 #endif 2289 if (qf_ptr->qf_viscol == TRUE) 2290 { 2291 /* 2292 * Check each character from the beginning of the error 2293 * line up to the error column. For each tab character 2294 * found, reduce the error column value by the length of 2295 * a tab character. 2296 */ 2297 line = ml_get_curline(); 2298 screen_col = 0; 2299 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col) 2300 { 2301 if (*line == NUL) 2302 break; 2303 if (*line++ == '\t') 2304 { 2305 curwin->w_cursor.col -= 7 - (screen_col % 8); 2306 screen_col += 8 - (screen_col % 8); 2307 } 2308 else 2309 ++screen_col; 2310 } 2311 } 2312 check_cursor(); 2313 } 2314 else 2315 beginline(BL_WHITE | BL_FIX); 2316 } 2317 else 2318 { 2319 pos_T save_cursor; 2320 2321 /* Move the cursor to the first line in the buffer */ 2322 save_cursor = curwin->w_cursor; 2323 curwin->w_cursor.lnum = 0; 2324 if (!do_search(NULL, '/', qf_ptr->qf_pattern, (long)1, 2325 SEARCH_KEEP, NULL)) 2326 curwin->w_cursor = save_cursor; 2327 } 2328 2329 #ifdef FEAT_FOLDING 2330 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped) 2331 foldOpenCursor(); 2332 #endif 2333 if (print_message) 2334 { 2335 /* Update the screen before showing the message, unless the screen 2336 * scrolled up. */ 2337 if (!msg_scrolled) 2338 update_topline_redraw(); 2339 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index, 2340 qi->qf_lists[qi->qf_curlist].qf_count, 2341 qf_ptr->qf_cleared ? _(" (line deleted)") : "", 2342 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr)); 2343 /* Add the message, skipping leading whitespace and newlines. */ 2344 len = (int)STRLEN(IObuff); 2345 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len); 2346 2347 /* Output the message. Overwrite to avoid scrolling when the 'O' 2348 * flag is present in 'shortmess'; But when not jumping, print the 2349 * whole message. */ 2350 i = msg_scroll; 2351 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum) 2352 msg_scroll = TRUE; 2353 else if (!msg_scrolled && shortmess(SHM_OVERALL)) 2354 msg_scroll = FALSE; 2355 msg_attr_keep(IObuff, 0, TRUE); 2356 msg_scroll = i; 2357 } 2358 } 2359 else 2360 { 2361 #ifdef FEAT_WINDOWS 2362 if (opened_window) 2363 win_close(curwin, TRUE); /* Close opened window */ 2364 #endif 2365 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0) 2366 { 2367 /* 2368 * Couldn't open file, so put index back where it was. This could 2369 * happen if the file was readonly and we changed something. 2370 */ 2371 #ifdef FEAT_WINDOWS 2372 failed: 2373 #endif 2374 qf_ptr = old_qf_ptr; 2375 qf_index = old_qf_index; 2376 } 2377 } 2378 theend: 2379 if (qi != NULL) 2380 { 2381 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr; 2382 qi->qf_lists[qi->qf_curlist].qf_index = qf_index; 2383 } 2384 #ifdef FEAT_WINDOWS 2385 if (p_swb != old_swb && opened_window) 2386 { 2387 /* Restore old 'switchbuf' value, but not when an autocommand or 2388 * modeline has changed the value. */ 2389 if (p_swb == empty_option) 2390 { 2391 p_swb = old_swb; 2392 swb_flags = old_swb_flags; 2393 } 2394 else 2395 free_string_option(old_swb); 2396 } 2397 #endif 2398 } 2399 2400 /* 2401 * ":clist": list all errors 2402 * ":llist": list all locations 2403 */ 2404 void 2405 qf_list(exarg_T *eap) 2406 { 2407 buf_T *buf; 2408 char_u *fname; 2409 qfline_T *qfp; 2410 int i; 2411 int idx1 = 1; 2412 int idx2 = -1; 2413 char_u *arg = eap->arg; 2414 int plus = FALSE; 2415 int all = eap->forceit; /* if not :cl!, only show 2416 recognised errors */ 2417 qf_info_T *qi = &ql_info; 2418 2419 if (eap->cmdidx == CMD_llist) 2420 { 2421 qi = GET_LOC_LIST(curwin); 2422 if (qi == NULL) 2423 { 2424 EMSG(_(e_loclist)); 2425 return; 2426 } 2427 } 2428 2429 if (qi->qf_curlist >= qi->qf_listcount 2430 || qi->qf_lists[qi->qf_curlist].qf_count == 0) 2431 { 2432 EMSG(_(e_quickfix)); 2433 return; 2434 } 2435 if (*arg == '+') 2436 { 2437 ++arg; 2438 plus = TRUE; 2439 } 2440 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL) 2441 { 2442 EMSG(_(e_trailing)); 2443 return; 2444 } 2445 if (plus) 2446 { 2447 i = qi->qf_lists[qi->qf_curlist].qf_index; 2448 idx2 = i + idx1; 2449 idx1 = i; 2450 } 2451 else 2452 { 2453 i = qi->qf_lists[qi->qf_curlist].qf_count; 2454 if (idx1 < 0) 2455 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1; 2456 if (idx2 < 0) 2457 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1; 2458 } 2459 2460 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid) 2461 all = TRUE; 2462 qfp = qi->qf_lists[qi->qf_curlist].qf_start; 2463 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ) 2464 { 2465 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2) 2466 { 2467 msg_putchar('\n'); 2468 if (got_int) 2469 break; 2470 2471 fname = NULL; 2472 if (qfp->qf_fnum != 0 2473 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL) 2474 { 2475 fname = buf->b_fname; 2476 if (qfp->qf_type == 1) /* :helpgrep */ 2477 fname = gettail(fname); 2478 } 2479 if (fname == NULL) 2480 sprintf((char *)IObuff, "%2d", i); 2481 else 2482 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", 2483 i, (char *)fname); 2484 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index 2485 ? hl_attr(HLF_L) : hl_attr(HLF_D)); 2486 if (qfp->qf_lnum == 0) 2487 IObuff[0] = NUL; 2488 else if (qfp->qf_col == 0) 2489 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum); 2490 else 2491 sprintf((char *)IObuff, ":%ld col %d", 2492 qfp->qf_lnum, qfp->qf_col); 2493 sprintf((char *)IObuff + STRLEN(IObuff), "%s:", 2494 (char *)qf_types(qfp->qf_type, qfp->qf_nr)); 2495 msg_puts_attr(IObuff, hl_attr(HLF_N)); 2496 if (qfp->qf_pattern != NULL) 2497 { 2498 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE); 2499 STRCAT(IObuff, ":"); 2500 msg_puts(IObuff); 2501 } 2502 msg_puts((char_u *)" "); 2503 2504 /* Remove newlines and leading whitespace from the text. For an 2505 * unrecognized line keep the indent, the compiler may mark a word 2506 * with ^^^^. */ 2507 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0) 2508 ? skipwhite(qfp->qf_text) : qfp->qf_text, 2509 IObuff, IOSIZE); 2510 msg_prt_line(IObuff, FALSE); 2511 out_flush(); /* show one line at a time */ 2512 } 2513 2514 qfp = qfp->qf_next; 2515 if (qfp == NULL) 2516 break; 2517 ++i; 2518 ui_breakcheck(); 2519 } 2520 } 2521 2522 /* 2523 * Remove newlines and leading whitespace from an error message. 2524 * Put the result in "buf[bufsize]". 2525 */ 2526 static void 2527 qf_fmt_text(char_u *text, char_u *buf, int bufsize) 2528 { 2529 int i; 2530 char_u *p = text; 2531 2532 for (i = 0; *p != NUL && i < bufsize - 1; ++i) 2533 { 2534 if (*p == '\n') 2535 { 2536 buf[i] = ' '; 2537 while (*++p != NUL) 2538 if (!vim_iswhite(*p) && *p != '\n') 2539 break; 2540 } 2541 else 2542 buf[i] = *p++; 2543 } 2544 buf[i] = NUL; 2545 } 2546 2547 /* 2548 * ":colder [count]": Up in the quickfix stack. 2549 * ":cnewer [count]": Down in the quickfix stack. 2550 * ":lolder [count]": Up in the location list stack. 2551 * ":lnewer [count]": Down in the location list stack. 2552 */ 2553 void 2554 qf_age(exarg_T *eap) 2555 { 2556 qf_info_T *qi = &ql_info; 2557 int count; 2558 2559 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer) 2560 { 2561 qi = GET_LOC_LIST(curwin); 2562 if (qi == NULL) 2563 { 2564 EMSG(_(e_loclist)); 2565 return; 2566 } 2567 } 2568 2569 if (eap->addr_count != 0) 2570 count = eap->line2; 2571 else 2572 count = 1; 2573 while (count--) 2574 { 2575 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder) 2576 { 2577 if (qi->qf_curlist == 0) 2578 { 2579 EMSG(_("E380: At bottom of quickfix stack")); 2580 break; 2581 } 2582 --qi->qf_curlist; 2583 } 2584 else 2585 { 2586 if (qi->qf_curlist >= qi->qf_listcount - 1) 2587 { 2588 EMSG(_("E381: At top of quickfix stack")); 2589 break; 2590 } 2591 ++qi->qf_curlist; 2592 } 2593 } 2594 qf_msg(qi); 2595 } 2596 2597 static void 2598 qf_msg(qf_info_T *qi) 2599 { 2600 smsg((char_u *)_("error list %d of %d; %d errors"), 2601 qi->qf_curlist + 1, qi->qf_listcount, 2602 qi->qf_lists[qi->qf_curlist].qf_count); 2603 #ifdef FEAT_WINDOWS 2604 qf_update_buffer(qi, NULL); 2605 #endif 2606 } 2607 2608 /* 2609 * Free error list "idx". 2610 */ 2611 static void 2612 qf_free(qf_info_T *qi, int idx) 2613 { 2614 qfline_T *qfp; 2615 qfline_T *qfpnext; 2616 int stop = FALSE; 2617 2618 while (qi->qf_lists[idx].qf_count && qi->qf_lists[idx].qf_start != NULL) 2619 { 2620 qfp = qi->qf_lists[idx].qf_start; 2621 qfpnext = qfp->qf_next; 2622 if (qi->qf_lists[idx].qf_title != NULL && !stop) 2623 { 2624 vim_free(qfp->qf_text); 2625 stop = (qfp == qfpnext); 2626 vim_free(qfp->qf_pattern); 2627 vim_free(qfp); 2628 if (stop) 2629 /* Somehow qf_count may have an incorrect value, set it to 1 2630 * to avoid crashing when it's wrong. 2631 * TODO: Avoid qf_count being incorrect. */ 2632 qi->qf_lists[idx].qf_count = 1; 2633 } 2634 qi->qf_lists[idx].qf_start = qfpnext; 2635 --qi->qf_lists[idx].qf_count; 2636 } 2637 vim_free(qi->qf_lists[idx].qf_title); 2638 qi->qf_lists[idx].qf_title = NULL; 2639 qi->qf_lists[idx].qf_index = 0; 2640 2641 qf_clean_dir_stack(&qi->qf_dir_stack); 2642 qf_clean_dir_stack(&qi->qf_file_stack); 2643 } 2644 2645 /* 2646 * qf_mark_adjust: adjust marks 2647 */ 2648 void 2649 qf_mark_adjust( 2650 win_T *wp, 2651 linenr_T line1, 2652 linenr_T line2, 2653 long amount, 2654 long amount_after) 2655 { 2656 int i; 2657 qfline_T *qfp; 2658 int idx; 2659 qf_info_T *qi = &ql_info; 2660 int found_one = FALSE; 2661 2662 if (!curbuf->b_has_qf_entry) 2663 return; 2664 if (wp != NULL) 2665 { 2666 if (wp->w_llist == NULL) 2667 return; 2668 qi = wp->w_llist; 2669 } 2670 2671 for (idx = 0; idx < qi->qf_listcount; ++idx) 2672 if (qi->qf_lists[idx].qf_count) 2673 for (i = 0, qfp = qi->qf_lists[idx].qf_start; 2674 i < qi->qf_lists[idx].qf_count && qfp != NULL; 2675 ++i, qfp = qfp->qf_next) 2676 if (qfp->qf_fnum == curbuf->b_fnum) 2677 { 2678 found_one = TRUE; 2679 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2) 2680 { 2681 if (amount == MAXLNUM) 2682 qfp->qf_cleared = TRUE; 2683 else 2684 qfp->qf_lnum += amount; 2685 } 2686 else if (amount_after && qfp->qf_lnum > line2) 2687 qfp->qf_lnum += amount_after; 2688 } 2689 2690 if (!found_one) 2691 curbuf->b_has_qf_entry = FALSE; 2692 } 2693 2694 /* 2695 * Make a nice message out of the error character and the error number: 2696 * char number message 2697 * e or E 0 " error" 2698 * w or W 0 " warning" 2699 * i or I 0 " info" 2700 * 0 0 "" 2701 * other 0 " c" 2702 * e or E n " error n" 2703 * w or W n " warning n" 2704 * i or I n " info n" 2705 * 0 n " error n" 2706 * other n " c n" 2707 * 1 x "" :helpgrep 2708 */ 2709 static char_u * 2710 qf_types(int c, int nr) 2711 { 2712 static char_u buf[20]; 2713 static char_u cc[3]; 2714 char_u *p; 2715 2716 if (c == 'W' || c == 'w') 2717 p = (char_u *)" warning"; 2718 else if (c == 'I' || c == 'i') 2719 p = (char_u *)" info"; 2720 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0)) 2721 p = (char_u *)" error"; 2722 else if (c == 0 || c == 1) 2723 p = (char_u *)""; 2724 else 2725 { 2726 cc[0] = ' '; 2727 cc[1] = c; 2728 cc[2] = NUL; 2729 p = cc; 2730 } 2731 2732 if (nr <= 0) 2733 return p; 2734 2735 sprintf((char *)buf, "%s %3d", (char *)p, nr); 2736 return buf; 2737 } 2738 2739 #if defined(FEAT_WINDOWS) || defined(PROTO) 2740 /* 2741 * ":cwindow": open the quickfix window if we have errors to display, 2742 * close it if not. 2743 * ":lwindow": open the location list window if we have locations to display, 2744 * close it if not. 2745 */ 2746 void 2747 ex_cwindow(exarg_T *eap) 2748 { 2749 qf_info_T *qi = &ql_info; 2750 win_T *win; 2751 2752 if (eap->cmdidx == CMD_lwindow) 2753 { 2754 qi = GET_LOC_LIST(curwin); 2755 if (qi == NULL) 2756 return; 2757 } 2758 2759 /* Look for an existing quickfix window. */ 2760 win = qf_find_win(qi); 2761 2762 /* 2763 * If a quickfix window is open but we have no errors to display, 2764 * close the window. If a quickfix window is not open, then open 2765 * it if we have errors; otherwise, leave it closed. 2766 */ 2767 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid 2768 || qi->qf_lists[qi->qf_curlist].qf_count == 0 2769 || qi->qf_curlist >= qi->qf_listcount) 2770 { 2771 if (win != NULL) 2772 ex_cclose(eap); 2773 } 2774 else if (win == NULL) 2775 ex_copen(eap); 2776 } 2777 2778 /* 2779 * ":cclose": close the window showing the list of errors. 2780 * ":lclose": close the window showing the location list 2781 */ 2782 void 2783 ex_cclose(exarg_T *eap) 2784 { 2785 win_T *win = NULL; 2786 qf_info_T *qi = &ql_info; 2787 2788 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow) 2789 { 2790 qi = GET_LOC_LIST(curwin); 2791 if (qi == NULL) 2792 return; 2793 } 2794 2795 /* Find existing quickfix window and close it. */ 2796 win = qf_find_win(qi); 2797 if (win != NULL) 2798 win_close(win, FALSE); 2799 } 2800 2801 /* 2802 * ":copen": open a window that shows the list of errors. 2803 * ":lopen": open a window that shows the location list. 2804 */ 2805 void 2806 ex_copen(exarg_T *eap) 2807 { 2808 qf_info_T *qi = &ql_info; 2809 int height; 2810 win_T *win; 2811 tabpage_T *prevtab = curtab; 2812 buf_T *qf_buf; 2813 win_T *oldwin = curwin; 2814 2815 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) 2816 { 2817 qi = GET_LOC_LIST(curwin); 2818 if (qi == NULL) 2819 { 2820 EMSG(_(e_loclist)); 2821 return; 2822 } 2823 } 2824 2825 if (eap->addr_count != 0) 2826 height = eap->line2; 2827 else 2828 height = QF_WINHEIGHT; 2829 2830 reset_VIsual_and_resel(); /* stop Visual mode */ 2831 #ifdef FEAT_GUI 2832 need_mouse_correct = TRUE; 2833 #endif 2834 2835 /* 2836 * Find existing quickfix window, or open a new one. 2837 */ 2838 win = qf_find_win(qi); 2839 2840 if (win != NULL && cmdmod.tab == 0) 2841 { 2842 win_goto(win); 2843 if (eap->addr_count != 0) 2844 { 2845 if (cmdmod.split & WSP_VERT) 2846 { 2847 if (height != W_WIDTH(win)) 2848 win_setwidth(height); 2849 } 2850 else if (height != win->w_height) 2851 win_setheight(height); 2852 } 2853 } 2854 else 2855 { 2856 qf_buf = qf_find_buf(qi); 2857 2858 /* The current window becomes the previous window afterwards. */ 2859 win = curwin; 2860 2861 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow) 2862 && cmdmod.split == 0) 2863 /* Create the new window at the very bottom, except when 2864 * :belowright or :aboveleft is used. */ 2865 win_goto(lastwin); 2866 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL) 2867 return; /* not enough room for window */ 2868 RESET_BINDING(curwin); 2869 2870 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) 2871 { 2872 /* 2873 * For the location list window, create a reference to the 2874 * location list from the window 'win'. 2875 */ 2876 curwin->w_llist_ref = win->w_llist; 2877 win->w_llist->qf_refcount++; 2878 } 2879 2880 if (oldwin != curwin) 2881 oldwin = NULL; /* don't store info when in another window */ 2882 if (qf_buf != NULL) 2883 /* Use the existing quickfix buffer */ 2884 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE, 2885 ECMD_HIDE + ECMD_OLDBUF, oldwin); 2886 else 2887 { 2888 /* Create a new quickfix buffer */ 2889 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin); 2890 /* switch off 'swapfile' */ 2891 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); 2892 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix", 2893 OPT_LOCAL); 2894 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL); 2895 RESET_BINDING(curwin); 2896 #ifdef FEAT_DIFF 2897 curwin->w_p_diff = FALSE; 2898 #endif 2899 #ifdef FEAT_FOLDING 2900 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual", 2901 OPT_LOCAL); 2902 #endif 2903 } 2904 2905 /* Only set the height when still in the same tab page and there is no 2906 * window to the side. */ 2907 if (curtab == prevtab && curwin->w_width == Columns) 2908 win_setheight(height); 2909 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */ 2910 if (win_valid(win)) 2911 prevwin = win; 2912 } 2913 2914 qf_set_title_var(qi); 2915 2916 /* 2917 * Fill the buffer with the quickfix list. 2918 */ 2919 qf_fill_buffer(qi, curbuf, NULL); 2920 2921 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index; 2922 curwin->w_cursor.col = 0; 2923 check_cursor(); 2924 update_topline(); /* scroll to show the line */ 2925 } 2926 2927 /* 2928 * Move the cursor in the quickfix window to "lnum". 2929 */ 2930 static void 2931 qf_win_goto(win_T *win, linenr_T lnum) 2932 { 2933 win_T *old_curwin = curwin; 2934 2935 curwin = win; 2936 curbuf = win->w_buffer; 2937 curwin->w_cursor.lnum = lnum; 2938 curwin->w_cursor.col = 0; 2939 #ifdef FEAT_VIRTUALEDIT 2940 curwin->w_cursor.coladd = 0; 2941 #endif 2942 curwin->w_curswant = 0; 2943 update_topline(); /* scroll to show the line */ 2944 redraw_later(VALID); 2945 curwin->w_redr_status = TRUE; /* update ruler */ 2946 curwin = old_curwin; 2947 curbuf = curwin->w_buffer; 2948 } 2949 2950 /* 2951 * :cbottom/:lbottom commands. 2952 */ 2953 void 2954 ex_cbottom(exarg_T *eap UNUSED) 2955 { 2956 qf_info_T *qi = &ql_info; 2957 win_T *win; 2958 2959 if (eap->cmdidx == CMD_lbottom) 2960 { 2961 qi = GET_LOC_LIST(curwin); 2962 if (qi == NULL) 2963 { 2964 EMSG(_(e_loclist)); 2965 return; 2966 } 2967 } 2968 2969 win = qf_find_win(qi); 2970 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count) 2971 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count); 2972 } 2973 2974 /* 2975 * Return the number of the current entry (line number in the quickfix 2976 * window). 2977 */ 2978 linenr_T 2979 qf_current_entry(win_T *wp) 2980 { 2981 qf_info_T *qi = &ql_info; 2982 2983 if (IS_LL_WINDOW(wp)) 2984 /* In the location list window, use the referenced location list */ 2985 qi = wp->w_llist_ref; 2986 2987 return qi->qf_lists[qi->qf_curlist].qf_index; 2988 } 2989 2990 /* 2991 * Update the cursor position in the quickfix window to the current error. 2992 * Return TRUE if there is a quickfix window. 2993 */ 2994 static int 2995 qf_win_pos_update( 2996 qf_info_T *qi, 2997 int old_qf_index) /* previous qf_index or zero */ 2998 { 2999 win_T *win; 3000 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index; 3001 3002 /* 3003 * Put the cursor on the current error in the quickfix window, so that 3004 * it's viewable. 3005 */ 3006 win = qf_find_win(qi); 3007 if (win != NULL 3008 && qf_index <= win->w_buffer->b_ml.ml_line_count 3009 && old_qf_index != qf_index) 3010 { 3011 if (qf_index > old_qf_index) 3012 { 3013 win->w_redraw_top = old_qf_index; 3014 win->w_redraw_bot = qf_index; 3015 } 3016 else 3017 { 3018 win->w_redraw_top = qf_index; 3019 win->w_redraw_bot = old_qf_index; 3020 } 3021 qf_win_goto(win, qf_index); 3022 } 3023 return win != NULL; 3024 } 3025 3026 /* 3027 * Check whether the given window is displaying the specified quickfix/location 3028 * list buffer 3029 */ 3030 static int 3031 is_qf_win(win_T *win, qf_info_T *qi) 3032 { 3033 /* 3034 * A window displaying the quickfix buffer will have the w_llist_ref field 3035 * set to NULL. 3036 * A window displaying a location list buffer will have the w_llist_ref 3037 * pointing to the location list. 3038 */ 3039 if (bt_quickfix(win->w_buffer)) 3040 if ((qi == &ql_info && win->w_llist_ref == NULL) 3041 || (qi != &ql_info && win->w_llist_ref == qi)) 3042 return TRUE; 3043 3044 return FALSE; 3045 } 3046 3047 /* 3048 * Find a window displaying the quickfix/location list 'qi' 3049 * Searches in only the windows opened in the current tab. 3050 */ 3051 static win_T * 3052 qf_find_win(qf_info_T *qi) 3053 { 3054 win_T *win; 3055 3056 FOR_ALL_WINDOWS(win) 3057 if (is_qf_win(win, qi)) 3058 break; 3059 3060 return win; 3061 } 3062 3063 /* 3064 * Find a quickfix buffer. 3065 * Searches in windows opened in all the tabs. 3066 */ 3067 static buf_T * 3068 qf_find_buf(qf_info_T *qi) 3069 { 3070 tabpage_T *tp; 3071 win_T *win; 3072 3073 FOR_ALL_TAB_WINDOWS(tp, win) 3074 if (is_qf_win(win, qi)) 3075 return win->w_buffer; 3076 3077 return NULL; 3078 } 3079 3080 /* 3081 * Find the quickfix buffer. If it exists, update the contents. 3082 */ 3083 static void 3084 qf_update_buffer(qf_info_T *qi, qfline_T *old_last) 3085 { 3086 buf_T *buf; 3087 win_T *win; 3088 win_T *curwin_save; 3089 aco_save_T aco; 3090 3091 /* Check if a buffer for the quickfix list exists. Update it. */ 3092 buf = qf_find_buf(qi); 3093 if (buf != NULL) 3094 { 3095 linenr_T old_line_count = buf->b_ml.ml_line_count; 3096 3097 if (old_last == NULL) 3098 /* set curwin/curbuf to buf and save a few things */ 3099 aucmd_prepbuf(&aco, buf); 3100 3101 if ((win = qf_find_win(qi)) != NULL) 3102 { 3103 curwin_save = curwin; 3104 curwin = win; 3105 qf_set_title_var(qi); 3106 curwin = curwin_save; 3107 } 3108 3109 qf_fill_buffer(qi, buf, old_last); 3110 3111 if (old_last == NULL) 3112 { 3113 (void)qf_win_pos_update(qi, 0); 3114 3115 /* restore curwin/curbuf and a few other things */ 3116 aucmd_restbuf(&aco); 3117 } 3118 3119 /* Only redraw when added lines are visible. This avoids flickering 3120 * when the added lines are not visible. */ 3121 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline) 3122 redraw_buf_later(buf, NOT_VALID); 3123 } 3124 } 3125 3126 /* 3127 * Set "w:quickfix_title" if "qi" has a title. 3128 */ 3129 static void 3130 qf_set_title_var(qf_info_T *qi) 3131 { 3132 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL) 3133 set_internal_string_var((char_u *)"w:quickfix_title", 3134 qi->qf_lists[qi->qf_curlist].qf_title); 3135 } 3136 3137 /* 3138 * Fill current buffer with quickfix errors, replacing any previous contents. 3139 * curbuf must be the quickfix buffer! 3140 * If "old_last" is not NULL append the items after this one. 3141 * When "old_last" is NULL then "buf" must equal "curbuf"! Because 3142 * ml_delete() is used and autocommands will be triggered. 3143 */ 3144 static void 3145 qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last) 3146 { 3147 linenr_T lnum; 3148 qfline_T *qfp; 3149 buf_T *errbuf; 3150 int len; 3151 int old_KeyTyped = KeyTyped; 3152 3153 if (old_last == NULL) 3154 { 3155 if (buf != curbuf) 3156 { 3157 EMSG2(_(e_intern2), "qf_fill_buffer()"); 3158 return; 3159 } 3160 3161 /* delete all existing lines */ 3162 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0) 3163 (void)ml_delete((linenr_T)1, FALSE); 3164 } 3165 3166 /* Check if there is anything to display */ 3167 if (qi->qf_curlist < qi->qf_listcount) 3168 { 3169 /* Add one line for each error */ 3170 if (old_last == NULL) 3171 { 3172 qfp = qi->qf_lists[qi->qf_curlist].qf_start; 3173 lnum = 0; 3174 } 3175 else 3176 { 3177 qfp = old_last->qf_next; 3178 lnum = buf->b_ml.ml_line_count; 3179 } 3180 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count) 3181 { 3182 if (qfp->qf_fnum != 0 3183 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL 3184 && errbuf->b_fname != NULL) 3185 { 3186 if (qfp->qf_type == 1) /* :helpgrep */ 3187 STRCPY(IObuff, gettail(errbuf->b_fname)); 3188 else 3189 STRCPY(IObuff, errbuf->b_fname); 3190 len = (int)STRLEN(IObuff); 3191 } 3192 else 3193 len = 0; 3194 IObuff[len++] = '|'; 3195 3196 if (qfp->qf_lnum > 0) 3197 { 3198 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum); 3199 len += (int)STRLEN(IObuff + len); 3200 3201 if (qfp->qf_col > 0) 3202 { 3203 sprintf((char *)IObuff + len, " col %d", qfp->qf_col); 3204 len += (int)STRLEN(IObuff + len); 3205 } 3206 3207 sprintf((char *)IObuff + len, "%s", 3208 (char *)qf_types(qfp->qf_type, qfp->qf_nr)); 3209 len += (int)STRLEN(IObuff + len); 3210 } 3211 else if (qfp->qf_pattern != NULL) 3212 { 3213 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len); 3214 len += (int)STRLEN(IObuff + len); 3215 } 3216 IObuff[len++] = '|'; 3217 IObuff[len++] = ' '; 3218 3219 /* Remove newlines and leading whitespace from the text. 3220 * For an unrecognized line keep the indent, the compiler may 3221 * mark a word with ^^^^. */ 3222 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text, 3223 IObuff + len, IOSIZE - len); 3224 3225 if (ml_append_buf(buf, lnum, IObuff, 3226 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL) 3227 break; 3228 ++lnum; 3229 qfp = qfp->qf_next; 3230 if (qfp == NULL) 3231 break; 3232 } 3233 3234 if (old_last == NULL) 3235 /* Delete the empty line which is now at the end */ 3236 (void)ml_delete(lnum + 1, FALSE); 3237 } 3238 3239 /* correct cursor position */ 3240 check_lnums(TRUE); 3241 3242 if (old_last == NULL) 3243 { 3244 /* Set the 'filetype' to "qf" each time after filling the buffer. 3245 * This resembles reading a file into a buffer, it's more logical when 3246 * using autocommands. */ 3247 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL); 3248 curbuf->b_p_ma = FALSE; 3249 3250 #ifdef FEAT_AUTOCMD 3251 keep_filetype = TRUE; /* don't detect 'filetype' */ 3252 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL, 3253 FALSE, curbuf); 3254 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL, 3255 FALSE, curbuf); 3256 keep_filetype = FALSE; 3257 #endif 3258 /* make sure it will be redrawn */ 3259 redraw_curbuf_later(NOT_VALID); 3260 } 3261 3262 /* Restore KeyTyped, setting 'filetype' may reset it. */ 3263 KeyTyped = old_KeyTyped; 3264 } 3265 3266 #endif /* FEAT_WINDOWS */ 3267 3268 /* 3269 * Return TRUE if "buf" is the quickfix buffer. 3270 */ 3271 int 3272 bt_quickfix(buf_T *buf) 3273 { 3274 return buf != NULL && buf->b_p_bt[0] == 'q'; 3275 } 3276 3277 /* 3278 * Return TRUE if "buf" is a "nofile" or "acwrite" buffer. 3279 * This means the buffer name is not a file name. 3280 */ 3281 int 3282 bt_nofile(buf_T *buf) 3283 { 3284 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f') 3285 || buf->b_p_bt[0] == 'a'); 3286 } 3287 3288 /* 3289 * Return TRUE if "buf" is a "nowrite" or "nofile" buffer. 3290 */ 3291 int 3292 bt_dontwrite(buf_T *buf) 3293 { 3294 return buf != NULL && buf->b_p_bt[0] == 'n'; 3295 } 3296 3297 int 3298 bt_dontwrite_msg(buf_T *buf) 3299 { 3300 if (bt_dontwrite(buf)) 3301 { 3302 EMSG(_("E382: Cannot write, 'buftype' option is set")); 3303 return TRUE; 3304 } 3305 return FALSE; 3306 } 3307 3308 /* 3309 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide" 3310 * and 'bufhidden'. 3311 */ 3312 int 3313 buf_hide(buf_T *buf) 3314 { 3315 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */ 3316 switch (buf->b_p_bh[0]) 3317 { 3318 case 'u': /* "unload" */ 3319 case 'w': /* "wipe" */ 3320 case 'd': return FALSE; /* "delete" */ 3321 case 'h': return TRUE; /* "hide" */ 3322 } 3323 return (p_hid || cmdmod.hide); 3324 } 3325 3326 /* 3327 * Return TRUE when using ":vimgrep" for ":grep". 3328 */ 3329 int 3330 grep_internal(cmdidx_T cmdidx) 3331 { 3332 return ((cmdidx == CMD_grep 3333 || cmdidx == CMD_lgrep 3334 || cmdidx == CMD_grepadd 3335 || cmdidx == CMD_lgrepadd) 3336 && STRCMP("internal", 3337 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0); 3338 } 3339 3340 /* 3341 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd" 3342 */ 3343 void 3344 ex_make(exarg_T *eap) 3345 { 3346 char_u *fname; 3347 char_u *cmd; 3348 unsigned len; 3349 win_T *wp = NULL; 3350 qf_info_T *qi = &ql_info; 3351 int res; 3352 #ifdef FEAT_AUTOCMD 3353 char_u *au_name = NULL; 3354 3355 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */ 3356 if (grep_internal(eap->cmdidx)) 3357 { 3358 ex_vimgrep(eap); 3359 return; 3360 } 3361 3362 switch (eap->cmdidx) 3363 { 3364 case CMD_make: au_name = (char_u *)"make"; break; 3365 case CMD_lmake: au_name = (char_u *)"lmake"; break; 3366 case CMD_grep: au_name = (char_u *)"grep"; break; 3367 case CMD_lgrep: au_name = (char_u *)"lgrep"; break; 3368 case CMD_grepadd: au_name = (char_u *)"grepadd"; break; 3369 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break; 3370 default: break; 3371 } 3372 if (au_name != NULL) 3373 { 3374 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 3375 curbuf->b_fname, TRUE, curbuf); 3376 # ifdef FEAT_EVAL 3377 if (did_throw || force_abort) 3378 return; 3379 # endif 3380 } 3381 #endif 3382 3383 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep 3384 || eap->cmdidx == CMD_lgrepadd) 3385 wp = curwin; 3386 3387 autowrite_all(); 3388 fname = get_mef_name(); 3389 if (fname == NULL) 3390 return; 3391 mch_remove(fname); /* in case it's not unique */ 3392 3393 /* 3394 * If 'shellpipe' empty: don't redirect to 'errorfile'. 3395 */ 3396 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1; 3397 if (*p_sp != NUL) 3398 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3; 3399 cmd = alloc(len); 3400 if (cmd == NULL) 3401 return; 3402 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg, 3403 (char *)p_shq); 3404 if (*p_sp != NUL) 3405 append_redir(cmd, len, p_sp, fname); 3406 /* 3407 * Output a newline if there's something else than the :make command that 3408 * was typed (in which case the cursor is in column 0). 3409 */ 3410 if (msg_col == 0) 3411 msg_didout = FALSE; 3412 msg_start(); 3413 MSG_PUTS(":!"); 3414 msg_outtrans(cmd); /* show what we are doing */ 3415 3416 /* let the shell know if we are redirecting output or not */ 3417 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0); 3418 3419 #ifdef AMIGA 3420 out_flush(); 3421 /* read window status report and redraw before message */ 3422 (void)char_avail(); 3423 #endif 3424 3425 res = qf_init(wp, fname, (eap->cmdidx != CMD_make 3426 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm, 3427 (eap->cmdidx != CMD_grepadd 3428 && eap->cmdidx != CMD_lgrepadd), 3429 *eap->cmdlinep); 3430 if (wp != NULL) 3431 qi = GET_LOC_LIST(wp); 3432 #ifdef FEAT_AUTOCMD 3433 if (au_name != NULL) 3434 { 3435 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 3436 curbuf->b_fname, TRUE, curbuf); 3437 if (qi->qf_curlist < qi->qf_listcount) 3438 res = qi->qf_lists[qi->qf_curlist].qf_count; 3439 else 3440 res = 0; 3441 } 3442 #endif 3443 if (res > 0 && !eap->forceit) 3444 qf_jump(qi, 0, 0, FALSE); /* display first error */ 3445 3446 mch_remove(fname); 3447 vim_free(fname); 3448 vim_free(cmd); 3449 } 3450 3451 /* 3452 * Return the name for the errorfile, in allocated memory. 3453 * Find a new unique name when 'makeef' contains "##". 3454 * Returns NULL for error. 3455 */ 3456 static char_u * 3457 get_mef_name(void) 3458 { 3459 char_u *p; 3460 char_u *name; 3461 static int start = -1; 3462 static int off = 0; 3463 #ifdef HAVE_LSTAT 3464 stat_T sb; 3465 #endif 3466 3467 if (*p_mef == NUL) 3468 { 3469 name = vim_tempname('e', FALSE); 3470 if (name == NULL) 3471 EMSG(_(e_notmp)); 3472 return name; 3473 } 3474 3475 for (p = p_mef; *p; ++p) 3476 if (p[0] == '#' && p[1] == '#') 3477 break; 3478 3479 if (*p == NUL) 3480 return vim_strsave(p_mef); 3481 3482 /* Keep trying until the name doesn't exist yet. */ 3483 for (;;) 3484 { 3485 if (start == -1) 3486 start = mch_get_pid(); 3487 else 3488 off += 19; 3489 3490 name = alloc((unsigned)STRLEN(p_mef) + 30); 3491 if (name == NULL) 3492 break; 3493 STRCPY(name, p_mef); 3494 sprintf((char *)name + (p - p_mef), "%d%d", start, off); 3495 STRCAT(name, p + 2); 3496 if (mch_getperm(name) < 0 3497 #ifdef HAVE_LSTAT 3498 /* Don't accept a symbolic link, its a security risk. */ 3499 && mch_lstat((char *)name, &sb) < 0 3500 #endif 3501 ) 3502 break; 3503 vim_free(name); 3504 } 3505 return name; 3506 } 3507 3508 /* 3509 * Returns the number of valid entries in the current quickfix/location list. 3510 */ 3511 int 3512 qf_get_size(exarg_T *eap) 3513 { 3514 qf_info_T *qi = &ql_info; 3515 qfline_T *qfp; 3516 int i, sz = 0; 3517 int prev_fnum = 0; 3518 3519 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3520 { 3521 /* Location list */ 3522 qi = GET_LOC_LIST(curwin); 3523 if (qi == NULL) 3524 return 0; 3525 } 3526 3527 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start; 3528 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL; 3529 ++i, qfp = qfp->qf_next) 3530 { 3531 if (qfp->qf_valid) 3532 { 3533 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo) 3534 sz++; /* Count all valid entries */ 3535 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3536 { 3537 /* Count the number of files */ 3538 sz++; 3539 prev_fnum = qfp->qf_fnum; 3540 } 3541 } 3542 } 3543 3544 return sz; 3545 } 3546 3547 /* 3548 * Returns the current index of the quickfix/location list. 3549 * Returns 0 if there is an error. 3550 */ 3551 int 3552 qf_get_cur_idx(exarg_T *eap) 3553 { 3554 qf_info_T *qi = &ql_info; 3555 3556 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3557 { 3558 /* Location list */ 3559 qi = GET_LOC_LIST(curwin); 3560 if (qi == NULL) 3561 return 0; 3562 } 3563 3564 return qi->qf_lists[qi->qf_curlist].qf_index; 3565 } 3566 3567 /* 3568 * Returns the current index in the quickfix/location list (counting only valid 3569 * entries). If no valid entries are in the list, then returns 1. 3570 */ 3571 int 3572 qf_get_cur_valid_idx(exarg_T *eap) 3573 { 3574 qf_info_T *qi = &ql_info; 3575 qf_list_T *qfl; 3576 qfline_T *qfp; 3577 int i, eidx = 0; 3578 int prev_fnum = 0; 3579 3580 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3581 { 3582 /* Location list */ 3583 qi = GET_LOC_LIST(curwin); 3584 if (qi == NULL) 3585 return 1; 3586 } 3587 3588 qfl = &qi->qf_lists[qi->qf_curlist]; 3589 qfp = qfl->qf_start; 3590 3591 /* check if the list has valid errors */ 3592 if (qfl->qf_count <= 0 || qfl->qf_nonevalid) 3593 return 1; 3594 3595 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next) 3596 { 3597 if (qfp->qf_valid) 3598 { 3599 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 3600 { 3601 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3602 { 3603 /* Count the number of files */ 3604 eidx++; 3605 prev_fnum = qfp->qf_fnum; 3606 } 3607 } 3608 else 3609 eidx++; 3610 } 3611 } 3612 3613 return eidx ? eidx : 1; 3614 } 3615 3616 /* 3617 * Get the 'n'th valid error entry in the quickfix or location list. 3618 * Used by :cdo, :ldo, :cfdo and :lfdo commands. 3619 * For :cdo and :ldo returns the 'n'th valid error entry. 3620 * For :cfdo and :lfdo returns the 'n'th valid file entry. 3621 */ 3622 static int 3623 qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo) 3624 { 3625 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist]; 3626 qfline_T *qfp = qfl->qf_start; 3627 int i, eidx; 3628 int prev_fnum = 0; 3629 3630 /* check if the list has valid errors */ 3631 if (qfl->qf_count <= 0 || qfl->qf_nonevalid) 3632 return 1; 3633 3634 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL; 3635 i++, qfp = qfp->qf_next) 3636 { 3637 if (qfp->qf_valid) 3638 { 3639 if (fdo) 3640 { 3641 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3642 { 3643 /* Count the number of files */ 3644 eidx++; 3645 prev_fnum = qfp->qf_fnum; 3646 } 3647 } 3648 else 3649 eidx++; 3650 } 3651 3652 if (eidx == n) 3653 break; 3654 } 3655 3656 if (i <= qfl->qf_count) 3657 return i; 3658 else 3659 return 1; 3660 } 3661 3662 /* 3663 * ":cc", ":crewind", ":cfirst" and ":clast". 3664 * ":ll", ":lrewind", ":lfirst" and ":llast". 3665 * ":cdo", ":ldo", ":cfdo" and ":lfdo" 3666 */ 3667 void 3668 ex_cc(exarg_T *eap) 3669 { 3670 qf_info_T *qi = &ql_info; 3671 int errornr; 3672 3673 if (eap->cmdidx == CMD_ll 3674 || eap->cmdidx == CMD_lrewind 3675 || eap->cmdidx == CMD_lfirst 3676 || eap->cmdidx == CMD_llast 3677 || eap->cmdidx == CMD_ldo 3678 || eap->cmdidx == CMD_lfdo) 3679 { 3680 qi = GET_LOC_LIST(curwin); 3681 if (qi == NULL) 3682 { 3683 EMSG(_(e_loclist)); 3684 return; 3685 } 3686 } 3687 3688 if (eap->addr_count > 0) 3689 errornr = (int)eap->line2; 3690 else 3691 { 3692 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll) 3693 errornr = 0; 3694 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind 3695 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst) 3696 errornr = 1; 3697 else 3698 errornr = 32767; 3699 } 3700 3701 /* For cdo and ldo commands, jump to the nth valid error. 3702 * For cfdo and lfdo commands, jump to the nth valid file entry. 3703 */ 3704 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo || 3705 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 3706 errornr = qf_get_nth_valid_entry(qi, 3707 eap->addr_count > 0 ? (int)eap->line1 : 1, 3708 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo); 3709 3710 qf_jump(qi, 0, errornr, eap->forceit); 3711 } 3712 3713 /* 3714 * ":cnext", ":cnfile", ":cNext" and ":cprevious". 3715 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile". 3716 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands. 3717 */ 3718 void 3719 ex_cnext(exarg_T *eap) 3720 { 3721 qf_info_T *qi = &ql_info; 3722 int errornr; 3723 3724 if (eap->cmdidx == CMD_lnext 3725 || eap->cmdidx == CMD_lNext 3726 || eap->cmdidx == CMD_lprevious 3727 || eap->cmdidx == CMD_lnfile 3728 || eap->cmdidx == CMD_lNfile 3729 || eap->cmdidx == CMD_lpfile 3730 || eap->cmdidx == CMD_ldo 3731 || eap->cmdidx == CMD_lfdo) 3732 { 3733 qi = GET_LOC_LIST(curwin); 3734 if (qi == NULL) 3735 { 3736 EMSG(_(e_loclist)); 3737 return; 3738 } 3739 } 3740 3741 if (eap->addr_count > 0 && 3742 (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo && 3743 eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo)) 3744 errornr = (int)eap->line2; 3745 else 3746 errornr = 1; 3747 3748 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext 3749 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo) 3750 ? FORWARD 3751 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile 3752 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 3753 ? FORWARD_FILE 3754 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile 3755 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile) 3756 ? BACKWARD_FILE 3757 : BACKWARD, 3758 errornr, eap->forceit); 3759 } 3760 3761 /* 3762 * ":cfile"/":cgetfile"/":caddfile" commands. 3763 * ":lfile"/":lgetfile"/":laddfile" commands. 3764 */ 3765 void 3766 ex_cfile(exarg_T *eap) 3767 { 3768 win_T *wp = NULL; 3769 qf_info_T *qi = &ql_info; 3770 #ifdef FEAT_AUTOCMD 3771 char_u *au_name = NULL; 3772 #endif 3773 3774 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile 3775 || eap->cmdidx == CMD_laddfile) 3776 wp = curwin; 3777 3778 #ifdef FEAT_AUTOCMD 3779 switch (eap->cmdidx) 3780 { 3781 case CMD_cfile: au_name = (char_u *)"cfile"; break; 3782 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break; 3783 case CMD_caddfile: au_name = (char_u *)"caddfile"; break; 3784 case CMD_lfile: au_name = (char_u *)"lfile"; break; 3785 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break; 3786 case CMD_laddfile: au_name = (char_u *)"laddfile"; break; 3787 default: break; 3788 } 3789 if (au_name != NULL) 3790 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf); 3791 #endif 3792 #ifdef FEAT_BROWSE 3793 if (cmdmod.browse) 3794 { 3795 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg, 3796 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL); 3797 if (browse_file == NULL) 3798 return; 3799 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0); 3800 vim_free(browse_file); 3801 } 3802 else 3803 #endif 3804 if (*eap->arg != NUL) 3805 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0); 3806 3807 /* 3808 * This function is used by the :cfile, :cgetfile and :caddfile 3809 * commands. 3810 * :cfile always creates a new quickfix list and jumps to the 3811 * first error. 3812 * :cgetfile creates a new quickfix list but doesn't jump to the 3813 * first error. 3814 * :caddfile adds to an existing quickfix list. If there is no 3815 * quickfix list then a new list is created. 3816 */ 3817 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile 3818 && eap->cmdidx != CMD_laddfile), 3819 *eap->cmdlinep) > 0 3820 && (eap->cmdidx == CMD_cfile 3821 || eap->cmdidx == CMD_lfile)) 3822 { 3823 #ifdef FEAT_AUTOCMD 3824 if (au_name != NULL) 3825 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); 3826 #endif 3827 if (wp != NULL) 3828 qi = GET_LOC_LIST(wp); 3829 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 3830 } 3831 3832 else 3833 { 3834 #ifdef FEAT_AUTOCMD 3835 if (au_name != NULL) 3836 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); 3837 #endif 3838 } 3839 } 3840 3841 /* 3842 * ":vimgrep {pattern} file(s)" 3843 * ":vimgrepadd {pattern} file(s)" 3844 * ":lvimgrep {pattern} file(s)" 3845 * ":lvimgrepadd {pattern} file(s)" 3846 */ 3847 void 3848 ex_vimgrep(exarg_T *eap) 3849 { 3850 regmmatch_T regmatch; 3851 int fcount; 3852 char_u **fnames; 3853 char_u *fname; 3854 char_u *title; 3855 char_u *s; 3856 char_u *p; 3857 int fi; 3858 qf_info_T *qi = &ql_info; 3859 #ifdef FEAT_AUTOCMD 3860 qfline_T *cur_qf_start; 3861 #endif 3862 long lnum; 3863 buf_T *buf; 3864 int duplicate_name = FALSE; 3865 int using_dummy; 3866 int redraw_for_dummy = FALSE; 3867 int found_match; 3868 buf_T *first_match_buf = NULL; 3869 time_t seconds = 0; 3870 int save_mls; 3871 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 3872 char_u *save_ei = NULL; 3873 #endif 3874 aco_save_T aco; 3875 int flags = 0; 3876 colnr_T col; 3877 long tomatch; 3878 char_u *dirname_start = NULL; 3879 char_u *dirname_now = NULL; 3880 char_u *target_dir = NULL; 3881 #ifdef FEAT_AUTOCMD 3882 char_u *au_name = NULL; 3883 3884 switch (eap->cmdidx) 3885 { 3886 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break; 3887 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break; 3888 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break; 3889 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break; 3890 case CMD_grep: au_name = (char_u *)"grep"; break; 3891 case CMD_lgrep: au_name = (char_u *)"lgrep"; break; 3892 case CMD_grepadd: au_name = (char_u *)"grepadd"; break; 3893 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break; 3894 default: break; 3895 } 3896 if (au_name != NULL) 3897 { 3898 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 3899 curbuf->b_fname, TRUE, curbuf); 3900 if (did_throw || force_abort) 3901 return; 3902 } 3903 #endif 3904 3905 if (eap->cmdidx == CMD_lgrep 3906 || eap->cmdidx == CMD_lvimgrep 3907 || eap->cmdidx == CMD_lgrepadd 3908 || eap->cmdidx == CMD_lvimgrepadd) 3909 { 3910 qi = ll_get_or_alloc_list(curwin); 3911 if (qi == NULL) 3912 return; 3913 } 3914 3915 if (eap->addr_count > 0) 3916 tomatch = eap->line2; 3917 else 3918 tomatch = MAXLNUM; 3919 3920 /* Get the search pattern: either white-separated or enclosed in // */ 3921 regmatch.regprog = NULL; 3922 title = vim_strsave(*eap->cmdlinep); 3923 p = skip_vimgrep_pat(eap->arg, &s, &flags); 3924 if (p == NULL) 3925 { 3926 EMSG(_(e_invalpat)); 3927 goto theend; 3928 } 3929 3930 if (s != NULL && *s == NUL) 3931 { 3932 /* Pattern is empty, use last search pattern. */ 3933 if (last_search_pat() == NULL) 3934 { 3935 EMSG(_(e_noprevre)); 3936 goto theend; 3937 } 3938 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); 3939 } 3940 else 3941 regmatch.regprog = vim_regcomp(s, RE_MAGIC); 3942 3943 if (regmatch.regprog == NULL) 3944 goto theend; 3945 regmatch.rmm_ic = p_ic; 3946 regmatch.rmm_maxcol = 0; 3947 3948 p = skipwhite(p); 3949 if (*p == NUL) 3950 { 3951 EMSG(_("E683: File name missing or invalid pattern")); 3952 goto theend; 3953 } 3954 3955 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd && 3956 eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd) 3957 || qi->qf_curlist == qi->qf_listcount) 3958 /* make place for a new list */ 3959 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep); 3960 3961 /* parse the list of arguments */ 3962 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL) 3963 goto theend; 3964 if (fcount == 0) 3965 { 3966 EMSG(_(e_nomatch)); 3967 goto theend; 3968 } 3969 3970 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start); 3971 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now); 3972 if (dirname_start == NULL || dirname_now == NULL) 3973 { 3974 FreeWild(fcount, fnames); 3975 goto theend; 3976 } 3977 3978 /* Remember the current directory, because a BufRead autocommand that does 3979 * ":lcd %:p:h" changes the meaning of short path names. */ 3980 mch_dirname(dirname_start, MAXPATHL); 3981 3982 #ifdef FEAT_AUTOCMD 3983 /* Remember the value of qf_start, so that we can check for autocommands 3984 * changing the current quickfix list. */ 3985 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 3986 #endif 3987 3988 seconds = (time_t)0; 3989 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi) 3990 { 3991 fname = shorten_fname1(fnames[fi]); 3992 if (time(NULL) > seconds) 3993 { 3994 /* Display the file name every second or so, show the user we are 3995 * working on it. */ 3996 seconds = time(NULL); 3997 msg_start(); 3998 p = msg_strtrunc(fname, TRUE); 3999 if (p == NULL) 4000 msg_outtrans(fname); 4001 else 4002 { 4003 msg_outtrans(p); 4004 vim_free(p); 4005 } 4006 msg_clr_eos(); 4007 msg_didout = FALSE; /* overwrite this message */ 4008 msg_nowait = TRUE; /* don't wait for this message */ 4009 msg_col = 0; 4010 out_flush(); 4011 } 4012 4013 buf = buflist_findname_exp(fnames[fi]); 4014 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 4015 { 4016 /* Remember that a buffer with this name already exists. */ 4017 duplicate_name = (buf != NULL); 4018 using_dummy = TRUE; 4019 redraw_for_dummy = TRUE; 4020 4021 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4022 /* Don't do Filetype autocommands to avoid loading syntax and 4023 * indent scripts, a great speed improvement. */ 4024 save_ei = au_event_disable(",Filetype"); 4025 #endif 4026 /* Don't use modelines here, it's useless. */ 4027 save_mls = p_mls; 4028 p_mls = 0; 4029 4030 /* Load file into a buffer, so that 'fileencoding' is detected, 4031 * autocommands applied, etc. */ 4032 buf = load_dummy_buffer(fname, dirname_start, dirname_now); 4033 4034 p_mls = save_mls; 4035 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4036 au_event_restore(save_ei); 4037 #endif 4038 } 4039 else 4040 /* Use existing, loaded buffer. */ 4041 using_dummy = FALSE; 4042 4043 #ifdef FEAT_AUTOCMD 4044 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start) 4045 { 4046 int idx; 4047 4048 /* Autocommands changed the quickfix list. Find the one we were 4049 * using and restore it. */ 4050 for (idx = 0; idx < LISTCOUNT; ++idx) 4051 if (cur_qf_start == qi->qf_lists[idx].qf_start) 4052 { 4053 qi->qf_curlist = idx; 4054 break; 4055 } 4056 if (idx == LISTCOUNT) 4057 { 4058 /* List cannot be found, create a new one. */ 4059 qf_new_list(qi, *eap->cmdlinep); 4060 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 4061 } 4062 } 4063 #endif 4064 4065 if (buf == NULL) 4066 { 4067 if (!got_int) 4068 smsg((char_u *)_("Cannot open file \"%s\""), fname); 4069 } 4070 else 4071 { 4072 /* Try for a match in all lines of the buffer. 4073 * For ":1vimgrep" look for first match only. */ 4074 found_match = FALSE; 4075 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; 4076 ++lnum) 4077 { 4078 col = 0; 4079 while (vim_regexec_multi(®match, curwin, buf, lnum, 4080 col, NULL) > 0) 4081 { 4082 ; 4083 if (qf_add_entry(qi, 4084 NULL, /* dir */ 4085 fname, 4086 0, 4087 ml_get_buf(buf, 4088 regmatch.startpos[0].lnum + lnum, FALSE), 4089 regmatch.startpos[0].lnum + lnum, 4090 regmatch.startpos[0].col + 1, 4091 FALSE, /* vis_col */ 4092 NULL, /* search pattern */ 4093 0, /* nr */ 4094 0, /* type */ 4095 TRUE /* valid */ 4096 ) == FAIL) 4097 { 4098 got_int = TRUE; 4099 break; 4100 } 4101 found_match = TRUE; 4102 if (--tomatch == 0) 4103 break; 4104 if ((flags & VGR_GLOBAL) == 0 4105 || regmatch.endpos[0].lnum > 0) 4106 break; 4107 col = regmatch.endpos[0].col 4108 + (col == regmatch.endpos[0].col); 4109 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE))) 4110 break; 4111 } 4112 line_breakcheck(); 4113 if (got_int) 4114 break; 4115 } 4116 #ifdef FEAT_AUTOCMD 4117 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 4118 #endif 4119 4120 if (using_dummy) 4121 { 4122 if (found_match && first_match_buf == NULL) 4123 first_match_buf = buf; 4124 if (duplicate_name) 4125 { 4126 /* Never keep a dummy buffer if there is another buffer 4127 * with the same name. */ 4128 wipe_dummy_buffer(buf, dirname_start); 4129 buf = NULL; 4130 } 4131 else if (!cmdmod.hide 4132 || buf->b_p_bh[0] == 'u' /* "unload" */ 4133 || buf->b_p_bh[0] == 'w' /* "wipe" */ 4134 || buf->b_p_bh[0] == 'd') /* "delete" */ 4135 { 4136 /* When no match was found we don't need to remember the 4137 * buffer, wipe it out. If there was a match and it 4138 * wasn't the first one or we won't jump there: only 4139 * unload the buffer. 4140 * Ignore 'hidden' here, because it may lead to having too 4141 * many swap files. */ 4142 if (!found_match) 4143 { 4144 wipe_dummy_buffer(buf, dirname_start); 4145 buf = NULL; 4146 } 4147 else if (buf != first_match_buf || (flags & VGR_NOJUMP)) 4148 { 4149 unload_dummy_buffer(buf, dirname_start); 4150 buf = NULL; 4151 } 4152 } 4153 4154 if (buf != NULL) 4155 { 4156 /* If the buffer is still loaded we need to use the 4157 * directory we jumped to below. */ 4158 if (buf == first_match_buf 4159 && target_dir == NULL 4160 && STRCMP(dirname_start, dirname_now) != 0) 4161 target_dir = vim_strsave(dirname_now); 4162 4163 /* The buffer is still loaded, the Filetype autocommands 4164 * need to be done now, in that buffer. And the modelines 4165 * need to be done (again). But not the window-local 4166 * options! */ 4167 aucmd_prepbuf(&aco, buf); 4168 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4169 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, 4170 buf->b_fname, TRUE, buf); 4171 #endif 4172 do_modelines(OPT_NOWIN); 4173 aucmd_restbuf(&aco); 4174 } 4175 } 4176 } 4177 } 4178 4179 FreeWild(fcount, fnames); 4180 4181 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 4182 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start; 4183 qi->qf_lists[qi->qf_curlist].qf_index = 1; 4184 4185 #ifdef FEAT_WINDOWS 4186 qf_update_buffer(qi, NULL); 4187 #endif 4188 4189 #ifdef FEAT_AUTOCMD 4190 if (au_name != NULL) 4191 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 4192 curbuf->b_fname, TRUE, curbuf); 4193 #endif 4194 4195 /* Jump to first match. */ 4196 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 4197 { 4198 if ((flags & VGR_NOJUMP) == 0) 4199 { 4200 buf = curbuf; 4201 qf_jump(qi, 0, 0, eap->forceit); 4202 if (buf != curbuf) 4203 /* If we jumped to another buffer redrawing will already be 4204 * taken care of. */ 4205 redraw_for_dummy = FALSE; 4206 4207 /* Jump to the directory used after loading the buffer. */ 4208 if (curbuf == first_match_buf && target_dir != NULL) 4209 { 4210 exarg_T ea; 4211 4212 ea.arg = target_dir; 4213 ea.cmdidx = CMD_lcd; 4214 ex_cd(&ea); 4215 } 4216 } 4217 } 4218 else 4219 EMSG2(_(e_nomatch2), s); 4220 4221 /* If we loaded a dummy buffer into the current window, the autocommands 4222 * may have messed up things, need to redraw and recompute folds. */ 4223 if (redraw_for_dummy) 4224 { 4225 #ifdef FEAT_FOLDING 4226 foldUpdateAll(curwin); 4227 #else 4228 redraw_later(NOT_VALID); 4229 #endif 4230 } 4231 4232 theend: 4233 vim_free(title); 4234 vim_free(dirname_now); 4235 vim_free(dirname_start); 4236 vim_free(target_dir); 4237 vim_regfree(regmatch.regprog); 4238 } 4239 4240 /* 4241 * Skip over the pattern argument of ":vimgrep /pat/[g][j]". 4242 * Put the start of the pattern in "*s", unless "s" is NULL. 4243 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP. 4244 * If "s" is not NULL terminate the pattern with a NUL. 4245 * Return a pointer to the char just past the pattern plus flags. 4246 */ 4247 char_u * 4248 skip_vimgrep_pat(char_u *p, char_u **s, int *flags) 4249 { 4250 int c; 4251 4252 if (vim_isIDc(*p)) 4253 { 4254 /* ":vimgrep pattern fname" */ 4255 if (s != NULL) 4256 *s = p; 4257 p = skiptowhite(p); 4258 if (s != NULL && *p != NUL) 4259 *p++ = NUL; 4260 } 4261 else 4262 { 4263 /* ":vimgrep /pattern/[g][j] fname" */ 4264 if (s != NULL) 4265 *s = p + 1; 4266 c = *p; 4267 p = skip_regexp(p + 1, c, TRUE, NULL); 4268 if (*p != c) 4269 return NULL; 4270 4271 /* Truncate the pattern. */ 4272 if (s != NULL) 4273 *p = NUL; 4274 ++p; 4275 4276 /* Find the flags */ 4277 while (*p == 'g' || *p == 'j') 4278 { 4279 if (flags != NULL) 4280 { 4281 if (*p == 'g') 4282 *flags |= VGR_GLOBAL; 4283 else 4284 *flags |= VGR_NOJUMP; 4285 } 4286 ++p; 4287 } 4288 } 4289 return p; 4290 } 4291 4292 /* 4293 * Restore current working directory to "dirname_start" if they differ, taking 4294 * into account whether it is set locally or globally. 4295 */ 4296 static void 4297 restore_start_dir(char_u *dirname_start) 4298 { 4299 char_u *dirname_now = alloc(MAXPATHL); 4300 4301 if (NULL != dirname_now) 4302 { 4303 mch_dirname(dirname_now, MAXPATHL); 4304 if (STRCMP(dirname_start, dirname_now) != 0) 4305 { 4306 /* If the directory has changed, change it back by building up an 4307 * appropriate ex command and executing it. */ 4308 exarg_T ea; 4309 4310 ea.arg = dirname_start; 4311 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd; 4312 ex_cd(&ea); 4313 } 4314 vim_free(dirname_now); 4315 } 4316 } 4317 4318 /* 4319 * Load file "fname" into a dummy buffer and return the buffer pointer, 4320 * placing the directory resulting from the buffer load into the 4321 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller 4322 * prior to calling this function. Restores directory to "dirname_start" prior 4323 * to returning, if autocmds or the 'autochdir' option have changed it. 4324 * 4325 * If creating the dummy buffer does not fail, must call unload_dummy_buffer() 4326 * or wipe_dummy_buffer() later! 4327 * 4328 * Returns NULL if it fails. 4329 */ 4330 static buf_T * 4331 load_dummy_buffer( 4332 char_u *fname, 4333 char_u *dirname_start, /* in: old directory */ 4334 char_u *resulting_dir) /* out: new directory */ 4335 { 4336 buf_T *newbuf; 4337 bufref_T newbufref; 4338 bufref_T newbuf_to_wipe; 4339 int failed = TRUE; 4340 aco_save_T aco; 4341 4342 /* Allocate a buffer without putting it in the buffer list. */ 4343 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); 4344 if (newbuf == NULL) 4345 return NULL; 4346 set_bufref(&newbufref, newbuf); 4347 4348 /* Init the options. */ 4349 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); 4350 4351 /* need to open the memfile before putting the buffer in a window */ 4352 if (ml_open(newbuf) == OK) 4353 { 4354 /* set curwin/curbuf to buf and save a few things */ 4355 aucmd_prepbuf(&aco, newbuf); 4356 4357 /* Need to set the filename for autocommands. */ 4358 (void)setfname(curbuf, fname, NULL, FALSE); 4359 4360 /* Create swap file now to avoid the ATTENTION message. */ 4361 check_need_swap(TRUE); 4362 4363 /* Remove the "dummy" flag, otherwise autocommands may not 4364 * work. */ 4365 curbuf->b_flags &= ~BF_DUMMY; 4366 4367 newbuf_to_wipe.br_buf = NULL; 4368 if (readfile(fname, NULL, 4369 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, 4370 NULL, READ_NEW | READ_DUMMY) == OK 4371 && !got_int 4372 && !(curbuf->b_flags & BF_NEW)) 4373 { 4374 failed = FALSE; 4375 if (curbuf != newbuf) 4376 { 4377 /* Bloody autocommands changed the buffer! Can happen when 4378 * using netrw and editing a remote file. Use the current 4379 * buffer instead, delete the dummy one after restoring the 4380 * window stuff. */ 4381 set_bufref(&newbuf_to_wipe, newbuf); 4382 newbuf = curbuf; 4383 } 4384 } 4385 4386 /* restore curwin/curbuf and a few other things */ 4387 aucmd_restbuf(&aco); 4388 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) 4389 wipe_buffer(newbuf_to_wipe.br_buf, FALSE); 4390 4391 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't 4392 * skip it. */ 4393 newbuf->b_flags |= BF_DUMMY; 4394 } 4395 4396 /* 4397 * When autocommands/'autochdir' option changed directory: go back. 4398 * Let the caller know what the resulting dir was first, in case it is 4399 * important. 4400 */ 4401 mch_dirname(resulting_dir, MAXPATHL); 4402 restore_start_dir(dirname_start); 4403 4404 if (!bufref_valid(&newbufref)) 4405 return NULL; 4406 if (failed) 4407 { 4408 wipe_dummy_buffer(newbuf, dirname_start); 4409 return NULL; 4410 } 4411 return newbuf; 4412 } 4413 4414 /* 4415 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores 4416 * directory to "dirname_start" prior to returning, if autocmds or the 4417 * 'autochdir' option have changed it. 4418 */ 4419 static void 4420 wipe_dummy_buffer(buf_T *buf, char_u *dirname_start) 4421 { 4422 if (curbuf != buf) /* safety check */ 4423 { 4424 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4425 cleanup_T cs; 4426 4427 /* Reset the error/interrupt/exception state here so that aborting() 4428 * returns FALSE when wiping out the buffer. Otherwise it doesn't 4429 * work when got_int is set. */ 4430 enter_cleanup(&cs); 4431 #endif 4432 4433 wipe_buffer(buf, FALSE); 4434 4435 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4436 /* Restore the error/interrupt/exception state if not discarded by a 4437 * new aborting error, interrupt, or uncaught exception. */ 4438 leave_cleanup(&cs); 4439 #endif 4440 /* When autocommands/'autochdir' option changed directory: go back. */ 4441 restore_start_dir(dirname_start); 4442 } 4443 } 4444 4445 /* 4446 * Unload the dummy buffer that load_dummy_buffer() created. Restores 4447 * directory to "dirname_start" prior to returning, if autocmds or the 4448 * 'autochdir' option have changed it. 4449 */ 4450 static void 4451 unload_dummy_buffer(buf_T *buf, char_u *dirname_start) 4452 { 4453 if (curbuf != buf) /* safety check */ 4454 { 4455 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE); 4456 4457 /* When autocommands/'autochdir' option changed directory: go back. */ 4458 restore_start_dir(dirname_start); 4459 } 4460 } 4461 4462 #if defined(FEAT_EVAL) || defined(PROTO) 4463 /* 4464 * Add each quickfix error to list "list" as a dictionary. 4465 */ 4466 int 4467 get_errorlist(win_T *wp, list_T *list) 4468 { 4469 qf_info_T *qi = &ql_info; 4470 dict_T *dict; 4471 char_u buf[2]; 4472 qfline_T *qfp; 4473 int i; 4474 int bufnum; 4475 4476 if (wp != NULL) 4477 { 4478 qi = GET_LOC_LIST(wp); 4479 if (qi == NULL) 4480 return FAIL; 4481 } 4482 4483 if (qi->qf_curlist >= qi->qf_listcount 4484 || qi->qf_lists[qi->qf_curlist].qf_count == 0) 4485 return FAIL; 4486 4487 qfp = qi->qf_lists[qi->qf_curlist].qf_start; 4488 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ++i) 4489 { 4490 /* Handle entries with a non-existing buffer number. */ 4491 bufnum = qfp->qf_fnum; 4492 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 4493 bufnum = 0; 4494 4495 if ((dict = dict_alloc()) == NULL) 4496 return FAIL; 4497 if (list_append_dict(list, dict) == FAIL) 4498 return FAIL; 4499 4500 buf[0] = qfp->qf_type; 4501 buf[1] = NUL; 4502 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL 4503 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL 4504 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL 4505 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL 4506 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL 4507 || dict_add_nr_str(dict, "pattern", 0L, 4508 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL 4509 || dict_add_nr_str(dict, "text", 0L, 4510 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL 4511 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL 4512 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL) 4513 return FAIL; 4514 4515 qfp = qfp->qf_next; 4516 if (qfp == NULL) 4517 break; 4518 } 4519 return OK; 4520 } 4521 4522 /* 4523 * Populate the quickfix list with the items supplied in the list 4524 * of dictionaries. "title" will be copied to w:quickfix_title. 4525 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list. 4526 */ 4527 int 4528 set_errorlist( 4529 win_T *wp, 4530 list_T *list, 4531 int action, 4532 char_u *title) 4533 { 4534 listitem_T *li; 4535 dict_T *d; 4536 char_u *filename, *pattern, *text, *type; 4537 int bufnum; 4538 long lnum; 4539 int col, nr; 4540 int vcol; 4541 #ifdef FEAT_WINDOWS 4542 qfline_T *old_last = NULL; 4543 #endif 4544 int valid, status; 4545 int retval = OK; 4546 qf_info_T *qi = &ql_info; 4547 int did_bufnr_emsg = FALSE; 4548 4549 if (wp != NULL) 4550 { 4551 qi = ll_get_or_alloc_list(wp); 4552 if (qi == NULL) 4553 return FAIL; 4554 } 4555 4556 if (action == ' ' || qi->qf_curlist == qi->qf_listcount) 4557 /* make place for a new list */ 4558 qf_new_list(qi, title); 4559 #ifdef FEAT_WINDOWS 4560 else if (action == 'a' && qi->qf_lists[qi->qf_curlist].qf_count > 0) 4561 /* Adding to existing list, use last entry. */ 4562 old_last = qi->qf_lists[qi->qf_curlist].qf_last; 4563 #endif 4564 else if (action == 'r') 4565 { 4566 qf_free(qi, qi->qf_curlist); 4567 qf_store_title(qi, title); 4568 } 4569 4570 for (li = list->lv_first; li != NULL; li = li->li_next) 4571 { 4572 if (li->li_tv.v_type != VAR_DICT) 4573 continue; /* Skip non-dict items */ 4574 4575 d = li->li_tv.vval.v_dict; 4576 if (d == NULL) 4577 continue; 4578 4579 filename = get_dict_string(d, (char_u *)"filename", TRUE); 4580 bufnum = (int)get_dict_number(d, (char_u *)"bufnr"); 4581 lnum = (int)get_dict_number(d, (char_u *)"lnum"); 4582 col = (int)get_dict_number(d, (char_u *)"col"); 4583 vcol = (int)get_dict_number(d, (char_u *)"vcol"); 4584 nr = (int)get_dict_number(d, (char_u *)"nr"); 4585 type = get_dict_string(d, (char_u *)"type", TRUE); 4586 pattern = get_dict_string(d, (char_u *)"pattern", TRUE); 4587 text = get_dict_string(d, (char_u *)"text", TRUE); 4588 if (text == NULL) 4589 text = vim_strsave((char_u *)""); 4590 4591 valid = TRUE; 4592 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL)) 4593 valid = FALSE; 4594 4595 /* Mark entries with non-existing buffer number as not valid. Give the 4596 * error message only once. */ 4597 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 4598 { 4599 if (!did_bufnr_emsg) 4600 { 4601 did_bufnr_emsg = TRUE; 4602 EMSGN(_("E92: Buffer %ld not found"), bufnum); 4603 } 4604 valid = FALSE; 4605 bufnum = 0; 4606 } 4607 4608 status = qf_add_entry(qi, 4609 NULL, /* dir */ 4610 filename, 4611 bufnum, 4612 text, 4613 lnum, 4614 col, 4615 vcol, /* vis_col */ 4616 pattern, /* search pattern */ 4617 nr, 4618 type == NULL ? NUL : *type, 4619 valid); 4620 4621 vim_free(filename); 4622 vim_free(pattern); 4623 vim_free(text); 4624 vim_free(type); 4625 4626 if (status == FAIL) 4627 { 4628 retval = FAIL; 4629 break; 4630 } 4631 } 4632 4633 if (qi->qf_lists[qi->qf_curlist].qf_index == 0) 4634 /* no valid entry */ 4635 qi->qf_lists[qi->qf_curlist].qf_nonevalid = TRUE; 4636 else 4637 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 4638 if (action != 'a') { 4639 qi->qf_lists[qi->qf_curlist].qf_ptr = 4640 qi->qf_lists[qi->qf_curlist].qf_start; 4641 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 4642 qi->qf_lists[qi->qf_curlist].qf_index = 1; 4643 } 4644 4645 #ifdef FEAT_WINDOWS 4646 /* Don't update the cursor in quickfix window when appending entries */ 4647 qf_update_buffer(qi, old_last); 4648 #endif 4649 4650 return retval; 4651 } 4652 #endif 4653 4654 /* 4655 * ":[range]cbuffer [bufnr]" command. 4656 * ":[range]caddbuffer [bufnr]" command. 4657 * ":[range]cgetbuffer [bufnr]" command. 4658 * ":[range]lbuffer [bufnr]" command. 4659 * ":[range]laddbuffer [bufnr]" command. 4660 * ":[range]lgetbuffer [bufnr]" command. 4661 */ 4662 void 4663 ex_cbuffer(exarg_T *eap) 4664 { 4665 buf_T *buf = NULL; 4666 qf_info_T *qi = &ql_info; 4667 4668 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer 4669 || eap->cmdidx == CMD_laddbuffer) 4670 { 4671 qi = ll_get_or_alloc_list(curwin); 4672 if (qi == NULL) 4673 return; 4674 } 4675 4676 if (*eap->arg == NUL) 4677 buf = curbuf; 4678 else if (*skipwhite(skipdigits(eap->arg)) == NUL) 4679 buf = buflist_findnr(atoi((char *)eap->arg)); 4680 if (buf == NULL) 4681 EMSG(_(e_invarg)); 4682 else if (buf->b_ml.ml_mfp == NULL) 4683 EMSG(_("E681: Buffer is not loaded")); 4684 else 4685 { 4686 if (eap->addr_count == 0) 4687 { 4688 eap->line1 = 1; 4689 eap->line2 = buf->b_ml.ml_line_count; 4690 } 4691 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count 4692 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count) 4693 EMSG(_(e_invrange)); 4694 else 4695 { 4696 char_u *qf_title = *eap->cmdlinep; 4697 4698 if (buf->b_sfname) 4699 { 4700 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)", 4701 (char *)qf_title, (char *)buf->b_sfname); 4702 qf_title = IObuff; 4703 } 4704 4705 if (qf_init_ext(qi, NULL, buf, NULL, p_efm, 4706 (eap->cmdidx != CMD_caddbuffer 4707 && eap->cmdidx != CMD_laddbuffer), 4708 eap->line1, eap->line2, 4709 qf_title) > 0 4710 && (eap->cmdidx == CMD_cbuffer 4711 || eap->cmdidx == CMD_lbuffer)) 4712 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 4713 } 4714 } 4715 } 4716 4717 #if defined(FEAT_EVAL) || defined(PROTO) 4718 /* 4719 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command. 4720 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command. 4721 */ 4722 void 4723 ex_cexpr(exarg_T *eap) 4724 { 4725 typval_T *tv; 4726 qf_info_T *qi = &ql_info; 4727 4728 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr 4729 || eap->cmdidx == CMD_laddexpr) 4730 { 4731 qi = ll_get_or_alloc_list(curwin); 4732 if (qi == NULL) 4733 return; 4734 } 4735 4736 /* Evaluate the expression. When the result is a string or a list we can 4737 * use it to fill the errorlist. */ 4738 tv = eval_expr(eap->arg, NULL); 4739 if (tv != NULL) 4740 { 4741 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL) 4742 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)) 4743 { 4744 if (qf_init_ext(qi, NULL, NULL, tv, p_efm, 4745 (eap->cmdidx != CMD_caddexpr 4746 && eap->cmdidx != CMD_laddexpr), 4747 (linenr_T)0, (linenr_T)0, *eap->cmdlinep) > 0 4748 && (eap->cmdidx == CMD_cexpr 4749 || eap->cmdidx == CMD_lexpr)) 4750 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 4751 } 4752 else 4753 EMSG(_("E777: String or List expected")); 4754 free_tv(tv); 4755 } 4756 } 4757 #endif 4758 4759 /* 4760 * ":helpgrep {pattern}" 4761 */ 4762 void 4763 ex_helpgrep(exarg_T *eap) 4764 { 4765 regmatch_T regmatch; 4766 char_u *save_cpo; 4767 char_u *p; 4768 int fcount; 4769 char_u **fnames; 4770 FILE *fd; 4771 int fi; 4772 long lnum; 4773 #ifdef FEAT_MULTI_LANG 4774 char_u *lang; 4775 #endif 4776 qf_info_T *qi = &ql_info; 4777 int new_qi = FALSE; 4778 win_T *wp; 4779 #ifdef FEAT_AUTOCMD 4780 char_u *au_name = NULL; 4781 #endif 4782 4783 #ifdef FEAT_MULTI_LANG 4784 /* Check for a specified language */ 4785 lang = check_help_lang(eap->arg); 4786 #endif 4787 4788 #ifdef FEAT_AUTOCMD 4789 switch (eap->cmdidx) 4790 { 4791 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break; 4792 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break; 4793 default: break; 4794 } 4795 if (au_name != NULL) 4796 { 4797 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 4798 curbuf->b_fname, TRUE, curbuf); 4799 if (did_throw || force_abort) 4800 return; 4801 } 4802 #endif 4803 4804 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 4805 save_cpo = p_cpo; 4806 p_cpo = empty_option; 4807 4808 if (eap->cmdidx == CMD_lhelpgrep) 4809 { 4810 /* Find an existing help window */ 4811 FOR_ALL_WINDOWS(wp) 4812 if (wp->w_buffer != NULL && wp->w_buffer->b_help) 4813 break; 4814 4815 if (wp == NULL) /* Help window not found */ 4816 qi = NULL; 4817 else 4818 qi = wp->w_llist; 4819 4820 if (qi == NULL) 4821 { 4822 /* Allocate a new location list for help text matches */ 4823 if ((qi = ll_new_list()) == NULL) 4824 return; 4825 new_qi = TRUE; 4826 } 4827 } 4828 4829 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING); 4830 regmatch.rm_ic = FALSE; 4831 if (regmatch.regprog != NULL) 4832 { 4833 #ifdef FEAT_MBYTE 4834 vimconv_T vc; 4835 4836 /* Help files are in utf-8 or latin1, convert lines when 'encoding' 4837 * differs. */ 4838 vc.vc_type = CONV_NONE; 4839 if (!enc_utf8) 4840 convert_setup(&vc, (char_u *)"utf-8", p_enc); 4841 #endif 4842 4843 /* create a new quickfix list */ 4844 qf_new_list(qi, *eap->cmdlinep); 4845 4846 /* Go through all directories in 'runtimepath' */ 4847 p = p_rtp; 4848 while (*p != NUL && !got_int) 4849 { 4850 copy_option_part(&p, NameBuff, MAXPATHL, ","); 4851 4852 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */ 4853 add_pathsep(NameBuff); 4854 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)"); 4855 if (gen_expand_wildcards(1, &NameBuff, &fcount, 4856 &fnames, EW_FILE|EW_SILENT) == OK 4857 && fcount > 0) 4858 { 4859 for (fi = 0; fi < fcount && !got_int; ++fi) 4860 { 4861 #ifdef FEAT_MULTI_LANG 4862 /* Skip files for a different language. */ 4863 if (lang != NULL 4864 && STRNICMP(lang, fnames[fi] 4865 + STRLEN(fnames[fi]) - 3, 2) != 0 4866 && !(STRNICMP(lang, "en", 2) == 0 4867 && STRNICMP("txt", fnames[fi] 4868 + STRLEN(fnames[fi]) - 3, 3) == 0)) 4869 continue; 4870 #endif 4871 fd = mch_fopen((char *)fnames[fi], "r"); 4872 if (fd != NULL) 4873 { 4874 lnum = 1; 4875 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) 4876 { 4877 char_u *line = IObuff; 4878 #ifdef FEAT_MBYTE 4879 /* Convert a line if 'encoding' is not utf-8 and 4880 * the line contains a non-ASCII character. */ 4881 if (vc.vc_type != CONV_NONE 4882 && has_non_ascii(IObuff)) { 4883 line = string_convert(&vc, IObuff, NULL); 4884 if (line == NULL) 4885 line = IObuff; 4886 } 4887 #endif 4888 4889 if (vim_regexec(®match, line, (colnr_T)0)) 4890 { 4891 int l = (int)STRLEN(line); 4892 4893 /* remove trailing CR, LF, spaces, etc. */ 4894 while (l > 0 && line[l - 1] <= ' ') 4895 line[--l] = NUL; 4896 4897 if (qf_add_entry(qi, 4898 NULL, /* dir */ 4899 fnames[fi], 4900 0, 4901 line, 4902 lnum, 4903 (int)(regmatch.startp[0] - line) 4904 + 1, /* col */ 4905 FALSE, /* vis_col */ 4906 NULL, /* search pattern */ 4907 0, /* nr */ 4908 1, /* type */ 4909 TRUE /* valid */ 4910 ) == FAIL) 4911 { 4912 got_int = TRUE; 4913 #ifdef FEAT_MBYTE 4914 if (line != IObuff) 4915 vim_free(line); 4916 #endif 4917 break; 4918 } 4919 } 4920 #ifdef FEAT_MBYTE 4921 if (line != IObuff) 4922 vim_free(line); 4923 #endif 4924 ++lnum; 4925 line_breakcheck(); 4926 } 4927 fclose(fd); 4928 } 4929 } 4930 FreeWild(fcount, fnames); 4931 } 4932 } 4933 4934 vim_regfree(regmatch.regprog); 4935 #ifdef FEAT_MBYTE 4936 if (vc.vc_type != CONV_NONE) 4937 convert_setup(&vc, NULL, NULL); 4938 #endif 4939 4940 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 4941 qi->qf_lists[qi->qf_curlist].qf_ptr = 4942 qi->qf_lists[qi->qf_curlist].qf_start; 4943 qi->qf_lists[qi->qf_curlist].qf_index = 1; 4944 } 4945 4946 if (p_cpo == empty_option) 4947 p_cpo = save_cpo; 4948 else 4949 /* Darn, some plugin changed the value. */ 4950 free_string_option(save_cpo); 4951 4952 #ifdef FEAT_WINDOWS 4953 qf_update_buffer(qi, NULL); 4954 #endif 4955 4956 #ifdef FEAT_AUTOCMD 4957 if (au_name != NULL) 4958 { 4959 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 4960 curbuf->b_fname, TRUE, curbuf); 4961 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL) 4962 /* autocommands made "qi" invalid */ 4963 return; 4964 } 4965 #endif 4966 4967 /* Jump to first match. */ 4968 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 4969 qf_jump(qi, 0, 0, FALSE); 4970 else 4971 EMSG2(_(e_nomatch2), eap->arg); 4972 4973 if (eap->cmdidx == CMD_lhelpgrep) 4974 { 4975 /* If the help window is not opened or if it already points to the 4976 * correct location list, then free the new location list. */ 4977 if (!curwin->w_buffer->b_help || curwin->w_llist == qi) 4978 { 4979 if (new_qi) 4980 ll_free_all(&qi); 4981 } 4982 else if (curwin->w_llist == NULL) 4983 curwin->w_llist = qi; 4984 } 4985 } 4986 4987 #endif /* FEAT_QUICKFIX */ 4988