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