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