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