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