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