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