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