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