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