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