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