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