1 /* vi:set ts=8 sts=4 sw=4 noet: 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 /* 25 * For each error the next struct is allocated and linked in a list. 26 */ 27 typedef struct qfline_S qfline_T; 28 struct qfline_S 29 { 30 qfline_T *qf_next; /* pointer to next error in the list */ 31 qfline_T *qf_prev; /* pointer to previous error in the list */ 32 linenr_T qf_lnum; /* line number where the error occurred */ 33 int qf_fnum; /* file number for the line */ 34 int qf_col; /* column where the error occurred */ 35 int qf_nr; /* error number */ 36 char_u *qf_pattern; /* search pattern for the error */ 37 char_u *qf_text; /* description of the error */ 38 char_u qf_viscol; /* set to TRUE if qf_col is screen column */ 39 char_u qf_cleared; /* set to TRUE if line has been deleted */ 40 char_u qf_type; /* type of the error (mostly 'E'); 1 for 41 :helpgrep */ 42 char_u qf_valid; /* valid error message detected */ 43 }; 44 45 /* 46 * There is a stack of error lists. 47 */ 48 #define LISTCOUNT 10 49 50 /* 51 * Quickfix/Location list definition 52 * Contains a list of entries (qfline_T). qf_start points to the first entry 53 * and qf_last points to the last entry. qf_count contains the list size. 54 * 55 * Usually the list contains one or more entries. But an empty list can be 56 * created using setqflist()/setloclist() with a title and/or user context 57 * information and entries can be added later using setqflist()/setloclist(). 58 */ 59 typedef struct qf_list_S 60 { 61 int_u qf_id; /* Unique identifier for this list */ 62 qfline_T *qf_start; /* pointer to the first error */ 63 qfline_T *qf_last; /* pointer to the last error */ 64 qfline_T *qf_ptr; /* pointer to the current error */ 65 int qf_count; /* number of errors (0 means empty list) */ 66 int qf_index; /* current index in the error list */ 67 int qf_nonevalid; /* TRUE if not a single valid entry found */ 68 char_u *qf_title; /* title derived from the command that created 69 * the error list or set by setqflist */ 70 typval_T *qf_ctx; /* context set by setqflist/setloclist */ 71 72 struct dir_stack_T *qf_dir_stack; 73 char_u *qf_directory; 74 struct dir_stack_T *qf_file_stack; 75 char_u *qf_currfile; 76 int qf_multiline; 77 int qf_multiignore; 78 int qf_multiscan; 79 } qf_list_T; 80 81 /* 82 * Quickfix/Location list stack definition 83 * Contains a list of quickfix/location lists (qf_list_T) 84 */ 85 struct qf_info_S 86 { 87 /* 88 * Count of references to this list. Used only for location lists. 89 * When a location list window reference this list, qf_refcount 90 * will be 2. Otherwise, qf_refcount will be 1. When qf_refcount 91 * reaches 0, the list is freed. 92 */ 93 int qf_refcount; 94 int qf_listcount; /* current number of lists */ 95 int qf_curlist; /* current error list */ 96 qf_list_T qf_lists[LISTCOUNT]; 97 }; 98 99 static qf_info_T ql_info; /* global quickfix list */ 100 static int_u last_qf_id = 0; /* Last used quickfix list id */ 101 102 #define FMT_PATTERNS 10 /* maximum number of % recognized */ 103 104 /* 105 * Structure used to hold the info of one part of 'errorformat' 106 */ 107 typedef struct efm_S efm_T; 108 struct efm_S 109 { 110 regprog_T *prog; /* pre-formatted part of 'errorformat' */ 111 efm_T *next; /* pointer to next (NULL if last) */ 112 char_u addr[FMT_PATTERNS]; /* indices of used % patterns */ 113 char_u prefix; /* prefix of this format line: */ 114 /* 'D' enter directory */ 115 /* 'X' leave directory */ 116 /* 'A' start of multi-line message */ 117 /* 'E' error message */ 118 /* 'W' warning message */ 119 /* 'I' informational message */ 120 /* 'C' continuation line */ 121 /* 'Z' end of multi-line message */ 122 /* 'G' general, unspecific message */ 123 /* 'P' push file (partial) message */ 124 /* 'Q' pop/quit file (partial) message */ 125 /* 'O' overread (partial) message */ 126 char_u flags; /* additional flags given in prefix */ 127 /* '-' do not include this line */ 128 /* '+' include whole line in message */ 129 int conthere; /* %> used */ 130 }; 131 132 static efm_T *fmt_start = NULL; /* cached across qf_parse_line() calls */ 133 134 static int qf_init_ext(qf_info_T *qi, int qf_idx, char_u *efile, buf_T *buf, typval_T *tv, char_u *errorformat, int newlist, linenr_T lnumfirst, linenr_T lnumlast, char_u *qf_title, char_u *enc); 135 static void qf_store_title(qf_info_T *qi, int qf_idx, char_u *title); 136 static void qf_new_list(qf_info_T *qi, char_u *qf_title); 137 static void ll_free_all(qf_info_T **pqi); 138 static int qf_add_entry(qf_info_T *qi, int qf_idx, char_u *dir, char_u *fname, int bufnum, char_u *mesg, long lnum, int col, int vis_col, char_u *pattern, int nr, int type, int valid); 139 static qf_info_T *ll_new_list(void); 140 static void qf_free(qf_info_T *qi, int idx); 141 static char_u *qf_types(int, int); 142 static int qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *, char_u *); 143 static char_u *qf_push_dir(char_u *, struct dir_stack_T **, int is_file_stack); 144 static char_u *qf_pop_dir(struct dir_stack_T **); 145 static char_u *qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *); 146 static void qf_fmt_text(char_u *text, char_u *buf, int bufsize); 147 static void qf_clean_dir_stack(struct dir_stack_T **); 148 static int qf_win_pos_update(qf_info_T *qi, int old_qf_index); 149 static int is_qf_win(win_T *win, qf_info_T *qi); 150 static win_T *qf_find_win(qf_info_T *qi); 151 static buf_T *qf_find_buf(qf_info_T *qi); 152 static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last); 153 static void qf_set_title_var(qf_info_T *qi); 154 static void qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last); 155 static char_u *get_mef_name(void); 156 static void restore_start_dir(char_u *dirname_start); 157 static buf_T *load_dummy_buffer(char_u *fname, char_u *dirname_start, char_u *resulting_dir); 158 static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start); 159 static void unload_dummy_buffer(buf_T *buf, char_u *dirname_start); 160 static qf_info_T *ll_get_or_alloc_list(win_T *); 161 162 /* Quickfix window check helper macro */ 163 #define IS_QF_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref == NULL) 164 /* Location list window check helper macro */ 165 #define IS_LL_WINDOW(wp) (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL) 166 /* 167 * Return location list for window 'wp' 168 * For location list window, return the referenced location list 169 */ 170 #define GET_LOC_LIST(wp) (IS_LL_WINDOW(wp) ? wp->w_llist_ref : wp->w_llist) 171 172 /* 173 * Looking up a buffer can be slow if there are many. Remember the last one 174 * to make this a lot faster if there are multiple matches in the same file. 175 */ 176 static char_u *qf_last_bufname = NULL; 177 static bufref_T qf_last_bufref = {NULL, 0, 0}; 178 179 /* 180 * Read the errorfile "efile" into memory, line by line, building the error 181 * list. Set the error list's title to qf_title. 182 * Return -1 for error, number of errors for success. 183 */ 184 int 185 qf_init(win_T *wp, 186 char_u *efile, 187 char_u *errorformat, 188 int newlist, /* TRUE: start a new error list */ 189 char_u *qf_title, 190 char_u *enc) 191 { 192 qf_info_T *qi = &ql_info; 193 194 if (wp != NULL) 195 { 196 qi = ll_get_or_alloc_list(wp); 197 if (qi == NULL) 198 return FAIL; 199 } 200 201 return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat, 202 newlist, (linenr_T)0, (linenr_T)0, qf_title, enc); 203 } 204 205 /* 206 * Maximum number of bytes allowed per line while reading a errorfile. 207 */ 208 #define LINE_MAXLEN 4096 209 210 static struct fmtpattern 211 { 212 char_u convchar; 213 char *pattern; 214 } fmt_pat[FMT_PATTERNS] = 215 { 216 {'f', ".\\+"}, /* only used when at end */ 217 {'n', "\\d\\+"}, 218 {'l', "\\d\\+"}, 219 {'c', "\\d\\+"}, 220 {'t', "."}, 221 {'m', ".\\+"}, 222 {'r', ".*"}, 223 {'p', "[- .]*"}, 224 {'v', "\\d\\+"}, 225 {'s', ".\\+"} 226 }; 227 228 /* 229 * Converts a 'errorformat' string to regular expression pattern 230 */ 231 static int 232 efm_to_regpat( 233 char_u *efm, 234 int len, 235 efm_T *fmt_ptr, 236 char_u *regpat, 237 char_u *errmsg) 238 { 239 char_u *ptr; 240 char_u *efmp; 241 char_u *srcptr; 242 int round; 243 int idx = 0; 244 245 /* 246 * Build regexp pattern from current 'errorformat' option 247 */ 248 ptr = regpat; 249 *ptr++ = '^'; 250 round = 0; 251 for (efmp = efm; efmp < efm + len; ++efmp) 252 { 253 if (*efmp == '%') 254 { 255 ++efmp; 256 for (idx = 0; idx < FMT_PATTERNS; ++idx) 257 if (fmt_pat[idx].convchar == *efmp) 258 break; 259 if (idx < FMT_PATTERNS) 260 { 261 if (fmt_ptr->addr[idx]) 262 { 263 sprintf((char *)errmsg, 264 _("E372: Too many %%%c in format string"), *efmp); 265 EMSG(errmsg); 266 return -1; 267 } 268 if ((idx 269 && idx < 6 270 && vim_strchr((char_u *)"DXOPQ", 271 fmt_ptr->prefix) != NULL) 272 || (idx == 6 273 && vim_strchr((char_u *)"OPQ", 274 fmt_ptr->prefix) == NULL)) 275 { 276 sprintf((char *)errmsg, 277 _("E373: Unexpected %%%c in format string"), *efmp); 278 EMSG(errmsg); 279 return -1; 280 } 281 fmt_ptr->addr[idx] = (char_u)++round; 282 *ptr++ = '\\'; 283 *ptr++ = '('; 284 #ifdef BACKSLASH_IN_FILENAME 285 if (*efmp == 'f') 286 { 287 /* Also match "c:" in the file name, even when 288 * checking for a colon next: "%f:". 289 * "\%(\a:\)\=" */ 290 STRCPY(ptr, "\\%(\\a:\\)\\="); 291 ptr += 10; 292 } 293 #endif 294 if (*efmp == 'f' && efmp[1] != NUL) 295 { 296 if (efmp[1] != '\\' && efmp[1] != '%') 297 { 298 /* A file name may contain spaces, but this isn't 299 * in "\f". For "%f:%l:%m" there may be a ":" in 300 * the file name. Use ".\{-1,}x" instead (x is 301 * the next character), the requirement that :999: 302 * follows should work. */ 303 STRCPY(ptr, ".\\{-1,}"); 304 ptr += 7; 305 } 306 else 307 { 308 /* File name followed by '\\' or '%': include as 309 * many file name chars as possible. */ 310 STRCPY(ptr, "\\f\\+"); 311 ptr += 4; 312 } 313 } 314 else 315 { 316 srcptr = (char_u *)fmt_pat[idx].pattern; 317 while ((*ptr = *srcptr++) != NUL) 318 ++ptr; 319 } 320 *ptr++ = '\\'; 321 *ptr++ = ')'; 322 } 323 else if (*efmp == '*') 324 { 325 if (*++efmp == '[' || *efmp == '\\') 326 { 327 if ((*ptr++ = *efmp) == '[') /* %*[^a-z0-9] etc. */ 328 { 329 if (efmp[1] == '^') 330 *ptr++ = *++efmp; 331 if (efmp < efm + len) 332 { 333 *ptr++ = *++efmp; /* could be ']' */ 334 while (efmp < efm + len 335 && (*ptr++ = *++efmp) != ']') 336 /* skip */; 337 if (efmp == efm + len) 338 { 339 EMSG(_("E374: Missing ] in format string")); 340 return -1; 341 } 342 } 343 } 344 else if (efmp < efm + len) /* %*\D, %*\s etc. */ 345 *ptr++ = *++efmp; 346 *ptr++ = '\\'; 347 *ptr++ = '+'; 348 } 349 else 350 { 351 /* TODO: scanf()-like: %*ud, %*3c, %*f, ... ? */ 352 sprintf((char *)errmsg, 353 _("E375: Unsupported %%%c in format string"), *efmp); 354 EMSG(errmsg); 355 return -1; 356 } 357 } 358 else if (vim_strchr((char_u *)"%\\.^$~[", *efmp) != NULL) 359 *ptr++ = *efmp; /* regexp magic characters */ 360 else if (*efmp == '#') 361 *ptr++ = '*'; 362 else if (*efmp == '>') 363 fmt_ptr->conthere = TRUE; 364 else if (efmp == efm + 1) /* analyse prefix */ 365 { 366 if (vim_strchr((char_u *)"+-", *efmp) != NULL) 367 fmt_ptr->flags = *efmp++; 368 if (vim_strchr((char_u *)"DXAEWICZGOPQ", *efmp) != NULL) 369 fmt_ptr->prefix = *efmp; 370 else 371 { 372 sprintf((char *)errmsg, 373 _("E376: Invalid %%%c in format string prefix"), *efmp); 374 EMSG(errmsg); 375 return -1; 376 } 377 } 378 else 379 { 380 sprintf((char *)errmsg, 381 _("E377: Invalid %%%c in format string"), *efmp); 382 EMSG(errmsg); 383 return -1; 384 } 385 } 386 else /* copy normal character */ 387 { 388 if (*efmp == '\\' && efmp + 1 < efm + len) 389 ++efmp; 390 else if (vim_strchr((char_u *)".*^$~[", *efmp) != NULL) 391 *ptr++ = '\\'; /* escape regexp atoms */ 392 if (*efmp) 393 *ptr++ = *efmp; 394 } 395 } 396 *ptr++ = '$'; 397 *ptr = NUL; 398 399 return 0; 400 } 401 402 static void 403 free_efm_list(efm_T **efm_first) 404 { 405 efm_T *efm_ptr; 406 407 for (efm_ptr = *efm_first; efm_ptr != NULL; efm_ptr = *efm_first) 408 { 409 *efm_first = efm_ptr->next; 410 vim_regfree(efm_ptr->prog); 411 vim_free(efm_ptr); 412 } 413 fmt_start = NULL; 414 } 415 416 /* Parse 'errorformat' option */ 417 static efm_T * 418 parse_efm_option(char_u *efm) 419 { 420 char_u *errmsg = NULL; 421 int errmsglen; 422 efm_T *fmt_ptr = NULL; 423 efm_T *fmt_first = NULL; 424 efm_T *fmt_last = NULL; 425 char_u *fmtstr = NULL; 426 int len; 427 int i; 428 int round; 429 430 errmsglen = CMDBUFFSIZE + 1; 431 errmsg = alloc_id(errmsglen, aid_qf_errmsg); 432 if (errmsg == NULL) 433 goto parse_efm_end; 434 435 /* 436 * Each part of the format string is copied and modified from errorformat 437 * to regex prog. Only a few % characters are allowed. 438 */ 439 440 /* 441 * Get some space to modify the format string into. 442 */ 443 i = (FMT_PATTERNS * 3) + ((int)STRLEN(efm) << 2); 444 for (round = FMT_PATTERNS; round > 0; ) 445 i += (int)STRLEN(fmt_pat[--round].pattern); 446 #ifdef BACKSLASH_IN_FILENAME 447 i += 12; /* "%f" can become twelve chars longer (see efm_to_regpat) */ 448 #else 449 i += 2; /* "%f" can become two chars longer */ 450 #endif 451 if ((fmtstr = alloc(i)) == NULL) 452 goto parse_efm_error; 453 454 while (efm[0] != NUL) 455 { 456 /* 457 * Allocate a new eformat structure and put it at the end of the list 458 */ 459 fmt_ptr = (efm_T *)alloc_clear((unsigned)sizeof(efm_T)); 460 if (fmt_ptr == NULL) 461 goto parse_efm_error; 462 if (fmt_first == NULL) /* first one */ 463 fmt_first = fmt_ptr; 464 else 465 fmt_last->next = fmt_ptr; 466 fmt_last = fmt_ptr; 467 468 /* 469 * Isolate one part in the 'errorformat' option 470 */ 471 for (len = 0; efm[len] != NUL && efm[len] != ','; ++len) 472 if (efm[len] == '\\' && efm[len + 1] != NUL) 473 ++len; 474 475 if (efm_to_regpat(efm, len, fmt_ptr, fmtstr, errmsg) == -1) 476 goto parse_efm_error; 477 if ((fmt_ptr->prog = vim_regcomp(fmtstr, RE_MAGIC + RE_STRING)) == NULL) 478 goto parse_efm_error; 479 /* 480 * Advance to next part 481 */ 482 efm = skip_to_option_part(efm + len); /* skip comma and spaces */ 483 } 484 485 if (fmt_first == NULL) /* nothing found */ 486 EMSG(_("E378: 'errorformat' contains no pattern")); 487 488 goto parse_efm_end; 489 490 parse_efm_error: 491 free_efm_list(&fmt_first); 492 493 parse_efm_end: 494 vim_free(fmtstr); 495 vim_free(errmsg); 496 497 return fmt_first; 498 } 499 500 enum { 501 QF_FAIL = 0, 502 QF_OK = 1, 503 QF_END_OF_INPUT = 2, 504 QF_NOMEM = 3, 505 QF_IGNORE_LINE = 4 506 }; 507 508 typedef struct { 509 char_u *linebuf; 510 int linelen; 511 char_u *growbuf; 512 int growbufsiz; 513 FILE *fd; 514 typval_T *tv; 515 char_u *p_str; 516 listitem_T *p_li; 517 buf_T *buf; 518 linenr_T buflnum; 519 linenr_T lnumlast; 520 vimconv_T vc; 521 } qfstate_T; 522 523 static char_u * 524 qf_grow_linebuf(qfstate_T *state, int newsz) 525 { 526 /* 527 * If the line exceeds LINE_MAXLEN exclude the last 528 * byte since it's not a NL character. 529 */ 530 state->linelen = newsz > LINE_MAXLEN ? LINE_MAXLEN - 1 : newsz; 531 if (state->growbuf == NULL) 532 { 533 state->growbuf = alloc(state->linelen + 1); 534 if (state->growbuf == NULL) 535 return NULL; 536 state->growbufsiz = state->linelen; 537 } 538 else if (state->linelen > state->growbufsiz) 539 { 540 state->growbuf = vim_realloc(state->growbuf, state->linelen + 1); 541 if (state->growbuf == NULL) 542 return NULL; 543 state->growbufsiz = state->linelen; 544 } 545 return state->growbuf; 546 } 547 548 /* 549 * Get the next string (separated by newline) from state->p_str. 550 */ 551 static int 552 qf_get_next_str_line(qfstate_T *state) 553 { 554 /* Get the next line from the supplied string */ 555 char_u *p_str = state->p_str; 556 char_u *p; 557 int len; 558 559 if (*p_str == NUL) /* Reached the end of the string */ 560 return QF_END_OF_INPUT; 561 562 p = vim_strchr(p_str, '\n'); 563 if (p != NULL) 564 len = (int)(p - p_str) + 1; 565 else 566 len = (int)STRLEN(p_str); 567 568 if (len > IOSIZE - 2) 569 { 570 state->linebuf = qf_grow_linebuf(state, len); 571 if (state->linebuf == NULL) 572 return QF_NOMEM; 573 } 574 else 575 { 576 state->linebuf = IObuff; 577 state->linelen = len; 578 } 579 vim_strncpy(state->linebuf, p_str, state->linelen); 580 581 /* 582 * Increment using len in order to discard the rest of the 583 * line if it exceeds LINE_MAXLEN. 584 */ 585 p_str += len; 586 state->p_str = p_str; 587 588 return QF_OK; 589 } 590 591 /* 592 * Get the next string from state->p_Li. 593 */ 594 static int 595 qf_get_next_list_line(qfstate_T *state) 596 { 597 listitem_T *p_li = state->p_li; 598 int len; 599 600 while (p_li != NULL 601 && (p_li->li_tv.v_type != VAR_STRING 602 || p_li->li_tv.vval.v_string == NULL)) 603 p_li = p_li->li_next; /* Skip non-string items */ 604 605 if (p_li == NULL) /* End of the list */ 606 { 607 state->p_li = NULL; 608 return QF_END_OF_INPUT; 609 } 610 611 len = (int)STRLEN(p_li->li_tv.vval.v_string); 612 if (len > IOSIZE - 2) 613 { 614 state->linebuf = qf_grow_linebuf(state, len); 615 if (state->linebuf == NULL) 616 return QF_NOMEM; 617 } 618 else 619 { 620 state->linebuf = IObuff; 621 state->linelen = len; 622 } 623 624 vim_strncpy(state->linebuf, p_li->li_tv.vval.v_string, state->linelen); 625 626 state->p_li = p_li->li_next; /* next item */ 627 return QF_OK; 628 } 629 630 /* 631 * Get the next string from state->buf. 632 */ 633 static int 634 qf_get_next_buf_line(qfstate_T *state) 635 { 636 char_u *p_buf = NULL; 637 int len; 638 639 /* Get the next line from the supplied buffer */ 640 if (state->buflnum > state->lnumlast) 641 return QF_END_OF_INPUT; 642 643 p_buf = ml_get_buf(state->buf, state->buflnum, FALSE); 644 state->buflnum += 1; 645 646 len = (int)STRLEN(p_buf); 647 if (len > IOSIZE - 2) 648 { 649 state->linebuf = qf_grow_linebuf(state, len); 650 if (state->linebuf == NULL) 651 return QF_NOMEM; 652 } 653 else 654 { 655 state->linebuf = IObuff; 656 state->linelen = len; 657 } 658 vim_strncpy(state->linebuf, p_buf, state->linelen); 659 660 return QF_OK; 661 } 662 663 /* 664 * Get the next string from file state->fd. 665 */ 666 static int 667 qf_get_next_file_line(qfstate_T *state) 668 { 669 int discard; 670 int growbuflen; 671 672 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL) 673 return QF_END_OF_INPUT; 674 675 discard = FALSE; 676 state->linelen = (int)STRLEN(IObuff); 677 if (state->linelen == IOSIZE - 1 && !(IObuff[state->linelen - 1] == '\n')) 678 { 679 /* 680 * The current line exceeds IObuff, continue reading using 681 * growbuf until EOL or LINE_MAXLEN bytes is read. 682 */ 683 if (state->growbuf == NULL) 684 { 685 state->growbufsiz = 2 * (IOSIZE - 1); 686 state->growbuf = alloc(state->growbufsiz); 687 if (state->growbuf == NULL) 688 return QF_NOMEM; 689 } 690 691 /* Copy the read part of the line, excluding null-terminator */ 692 memcpy(state->growbuf, IObuff, IOSIZE - 1); 693 growbuflen = state->linelen; 694 695 for (;;) 696 { 697 if (fgets((char *)state->growbuf + growbuflen, 698 state->growbufsiz - growbuflen, state->fd) == NULL) 699 break; 700 state->linelen = (int)STRLEN(state->growbuf + growbuflen); 701 growbuflen += state->linelen; 702 if ((state->growbuf)[growbuflen - 1] == '\n') 703 break; 704 if (state->growbufsiz == LINE_MAXLEN) 705 { 706 discard = TRUE; 707 break; 708 } 709 710 state->growbufsiz = 2 * state->growbufsiz < LINE_MAXLEN 711 ? 2 * state->growbufsiz : LINE_MAXLEN; 712 state->growbuf = vim_realloc(state->growbuf, state->growbufsiz); 713 if (state->growbuf == NULL) 714 return QF_NOMEM; 715 } 716 717 while (discard) 718 { 719 /* 720 * The current line is longer than LINE_MAXLEN, continue 721 * reading but discard everything until EOL or EOF is 722 * reached. 723 */ 724 if (fgets((char *)IObuff, IOSIZE, state->fd) == NULL 725 || (int)STRLEN(IObuff) < IOSIZE - 1 726 || IObuff[IOSIZE - 1] == '\n') 727 break; 728 } 729 730 state->linebuf = state->growbuf; 731 state->linelen = growbuflen; 732 } 733 else 734 state->linebuf = IObuff; 735 736 #ifdef FEAT_MBYTE 737 /* Convert a line if it contains a non-ASCII character. */ 738 if (state->vc.vc_type != CONV_NONE && has_non_ascii(state->linebuf)) 739 { 740 char_u *line; 741 742 line = string_convert(&state->vc, state->linebuf, &state->linelen); 743 if (line != NULL) 744 { 745 if (state->linelen < IOSIZE) 746 { 747 STRCPY(state->linebuf, line); 748 vim_free(line); 749 } 750 else 751 { 752 vim_free(state->growbuf); 753 state->linebuf = state->growbuf = line; 754 state->growbufsiz = state->linelen < LINE_MAXLEN 755 ? state->linelen : LINE_MAXLEN; 756 } 757 } 758 } 759 #endif 760 761 return QF_OK; 762 } 763 764 /* 765 * Get the next string from a file/buffer/list/string. 766 */ 767 static int 768 qf_get_nextline(qfstate_T *state) 769 { 770 int status = QF_FAIL; 771 772 if (state->fd == NULL) 773 { 774 if (state->tv != NULL) 775 { 776 if (state->tv->v_type == VAR_STRING) 777 /* Get the next line from the supplied string */ 778 status = qf_get_next_str_line(state); 779 else if (state->tv->v_type == VAR_LIST) 780 /* Get the next line from the supplied list */ 781 status = qf_get_next_list_line(state); 782 } 783 else 784 /* Get the next line from the supplied buffer */ 785 status = qf_get_next_buf_line(state); 786 } 787 else 788 /* Get the next line from the supplied file */ 789 status = qf_get_next_file_line(state); 790 791 if (status != QF_OK) 792 return status; 793 794 /* remove newline/CR from the line */ 795 if (state->linelen > 0 && state->linebuf[state->linelen - 1] == '\n') 796 { 797 state->linebuf[state->linelen - 1] = NUL; 798 #ifdef USE_CRNL 799 if (state->linelen > 1 && state->linebuf[state->linelen - 2] == '\r') 800 state->linebuf[state->linelen - 2] = NUL; 801 #endif 802 } 803 804 #ifdef FEAT_MBYTE 805 remove_bom(state->linebuf); 806 #endif 807 808 return QF_OK; 809 } 810 811 typedef struct { 812 char_u *namebuf; 813 char_u *errmsg; 814 int errmsglen; 815 long lnum; 816 int col; 817 char_u use_viscol; 818 char_u *pattern; 819 int enr; 820 int type; 821 int valid; 822 } qffields_T; 823 824 /* 825 * Parse a line and get the quickfix fields. 826 * Return the QF_ status. 827 */ 828 static int 829 qf_parse_line( 830 qf_info_T *qi, 831 int qf_idx, 832 char_u *linebuf, 833 int linelen, 834 efm_T *fmt_first, 835 qffields_T *fields) 836 { 837 efm_T *fmt_ptr; 838 char_u *ptr; 839 int len; 840 int i; 841 int idx = 0; 842 char_u *tail = NULL; 843 regmatch_T regmatch; 844 qf_list_T *qfl = &qi->qf_lists[qf_idx]; 845 846 /* Always ignore case when looking for a matching error. */ 847 regmatch.rm_ic = TRUE; 848 849 /* If there was no %> item start at the first pattern */ 850 if (fmt_start == NULL) 851 fmt_ptr = fmt_first; 852 else 853 { 854 fmt_ptr = fmt_start; 855 fmt_start = NULL; 856 } 857 858 /* 859 * Try to match each part of 'errorformat' until we find a complete 860 * match or no match. 861 */ 862 fields->valid = TRUE; 863 restofline: 864 for ( ; fmt_ptr != NULL; fmt_ptr = fmt_ptr->next) 865 { 866 int r; 867 868 idx = fmt_ptr->prefix; 869 if (qfl->qf_multiscan && vim_strchr((char_u *)"OPQ", idx) == NULL) 870 continue; 871 fields->namebuf[0] = NUL; 872 fields->pattern[0] = NUL; 873 if (!qfl->qf_multiscan) 874 fields->errmsg[0] = NUL; 875 fields->lnum = 0; 876 fields->col = 0; 877 fields->use_viscol = FALSE; 878 fields->enr = -1; 879 fields->type = 0; 880 tail = NULL; 881 882 regmatch.regprog = fmt_ptr->prog; 883 r = vim_regexec(®match, linebuf, (colnr_T)0); 884 fmt_ptr->prog = regmatch.regprog; 885 if (r) 886 { 887 if ((idx == 'C' || idx == 'Z') && !qfl->qf_multiline) 888 continue; 889 if (vim_strchr((char_u *)"EWI", idx) != NULL) 890 fields->type = idx; 891 else 892 fields->type = 0; 893 /* 894 * Extract error message data from matched line. 895 * We check for an actual submatch, because "\[" and "\]" in 896 * the 'errorformat' may cause the wrong submatch to be used. 897 */ 898 if ((i = (int)fmt_ptr->addr[0]) > 0) /* %f */ 899 { 900 int c; 901 902 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 903 continue; 904 905 /* Expand ~/file and $HOME/file to full path. */ 906 c = *regmatch.endp[i]; 907 *regmatch.endp[i] = NUL; 908 expand_env(regmatch.startp[i], fields->namebuf, CMDBUFFSIZE); 909 *regmatch.endp[i] = c; 910 911 if (vim_strchr((char_u *)"OPQ", idx) != NULL 912 && mch_getperm(fields->namebuf) == -1) 913 continue; 914 } 915 if ((i = (int)fmt_ptr->addr[1]) > 0) /* %n */ 916 { 917 if (regmatch.startp[i] == NULL) 918 continue; 919 fields->enr = (int)atol((char *)regmatch.startp[i]); 920 } 921 if ((i = (int)fmt_ptr->addr[2]) > 0) /* %l */ 922 { 923 if (regmatch.startp[i] == NULL) 924 continue; 925 fields->lnum = atol((char *)regmatch.startp[i]); 926 } 927 if ((i = (int)fmt_ptr->addr[3]) > 0) /* %c */ 928 { 929 if (regmatch.startp[i] == NULL) 930 continue; 931 fields->col = (int)atol((char *)regmatch.startp[i]); 932 } 933 if ((i = (int)fmt_ptr->addr[4]) > 0) /* %t */ 934 { 935 if (regmatch.startp[i] == NULL) 936 continue; 937 fields->type = *regmatch.startp[i]; 938 } 939 if (fmt_ptr->flags == '+' && !qfl->qf_multiscan) /* %+ */ 940 { 941 if (linelen >= fields->errmsglen) 942 { 943 /* linelen + null terminator */ 944 if ((fields->errmsg = vim_realloc(fields->errmsg, 945 linelen + 1)) == NULL) 946 return QF_NOMEM; 947 fields->errmsglen = linelen + 1; 948 } 949 vim_strncpy(fields->errmsg, linebuf, linelen); 950 } 951 else if ((i = (int)fmt_ptr->addr[5]) > 0) /* %m */ 952 { 953 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 954 continue; 955 len = (int)(regmatch.endp[i] - regmatch.startp[i]); 956 if (len >= fields->errmsglen) 957 { 958 /* len + null terminator */ 959 if ((fields->errmsg = vim_realloc(fields->errmsg, len + 1)) 960 == NULL) 961 return QF_NOMEM; 962 fields->errmsglen = len + 1; 963 } 964 vim_strncpy(fields->errmsg, regmatch.startp[i], len); 965 } 966 if ((i = (int)fmt_ptr->addr[6]) > 0) /* %r */ 967 { 968 if (regmatch.startp[i] == NULL) 969 continue; 970 tail = regmatch.startp[i]; 971 } 972 if ((i = (int)fmt_ptr->addr[7]) > 0) /* %p */ 973 { 974 char_u *match_ptr; 975 976 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 977 continue; 978 fields->col = 0; 979 for (match_ptr = regmatch.startp[i]; 980 match_ptr != regmatch.endp[i]; ++match_ptr) 981 { 982 ++fields->col; 983 if (*match_ptr == TAB) 984 { 985 fields->col += 7; 986 fields->col -= fields->col % 8; 987 } 988 } 989 ++fields->col; 990 fields->use_viscol = TRUE; 991 } 992 if ((i = (int)fmt_ptr->addr[8]) > 0) /* %v */ 993 { 994 if (regmatch.startp[i] == NULL) 995 continue; 996 fields->col = (int)atol((char *)regmatch.startp[i]); 997 fields->use_viscol = TRUE; 998 } 999 if ((i = (int)fmt_ptr->addr[9]) > 0) /* %s */ 1000 { 1001 if (regmatch.startp[i] == NULL || regmatch.endp[i] == NULL) 1002 continue; 1003 len = (int)(regmatch.endp[i] - regmatch.startp[i]); 1004 if (len > CMDBUFFSIZE - 5) 1005 len = CMDBUFFSIZE - 5; 1006 STRCPY(fields->pattern, "^\\V"); 1007 STRNCAT(fields->pattern, regmatch.startp[i], len); 1008 fields->pattern[len + 3] = '\\'; 1009 fields->pattern[len + 4] = '$'; 1010 fields->pattern[len + 5] = NUL; 1011 } 1012 break; 1013 } 1014 } 1015 qfl->qf_multiscan = FALSE; 1016 1017 if (fmt_ptr == NULL || idx == 'D' || idx == 'X') 1018 { 1019 if (fmt_ptr != NULL) 1020 { 1021 if (idx == 'D') /* enter directory */ 1022 { 1023 if (*fields->namebuf == NUL) 1024 { 1025 EMSG(_("E379: Missing or empty directory name")); 1026 return QF_FAIL; 1027 } 1028 qfl->qf_directory = 1029 qf_push_dir(fields->namebuf, &qfl->qf_dir_stack, FALSE); 1030 if (qfl->qf_directory == NULL) 1031 return QF_FAIL; 1032 } 1033 else if (idx == 'X') /* leave directory */ 1034 qfl->qf_directory = qf_pop_dir(&qfl->qf_dir_stack); 1035 } 1036 fields->namebuf[0] = NUL; /* no match found, remove file name */ 1037 fields->lnum = 0; /* don't jump to this line */ 1038 fields->valid = FALSE; 1039 if (linelen >= fields->errmsglen) 1040 { 1041 /* linelen + null terminator */ 1042 if ((fields->errmsg = vim_realloc(fields->errmsg, 1043 linelen + 1)) == NULL) 1044 return QF_NOMEM; 1045 fields->errmsglen = linelen + 1; 1046 } 1047 /* copy whole line to error message */ 1048 vim_strncpy(fields->errmsg, linebuf, linelen); 1049 if (fmt_ptr == NULL) 1050 qfl->qf_multiline = qfl->qf_multiignore = FALSE; 1051 } 1052 else if (fmt_ptr != NULL) 1053 { 1054 /* honor %> item */ 1055 if (fmt_ptr->conthere) 1056 fmt_start = fmt_ptr; 1057 1058 if (vim_strchr((char_u *)"AEWI", idx) != NULL) 1059 { 1060 qfl->qf_multiline = TRUE; /* start of a multi-line message */ 1061 qfl->qf_multiignore = FALSE;/* reset continuation */ 1062 } 1063 else if (vim_strchr((char_u *)"CZ", idx) != NULL) 1064 { /* continuation of multi-line msg */ 1065 if (!qfl->qf_multiignore) 1066 { 1067 qfline_T *qfprev = qfl->qf_last; 1068 1069 if (qfprev == NULL) 1070 return QF_FAIL; 1071 if (*fields->errmsg && !qfl->qf_multiignore) 1072 { 1073 len = (int)STRLEN(qfprev->qf_text); 1074 if ((ptr = alloc((unsigned)(len + STRLEN(fields->errmsg) + 2))) 1075 == NULL) 1076 return QF_FAIL; 1077 STRCPY(ptr, qfprev->qf_text); 1078 vim_free(qfprev->qf_text); 1079 qfprev->qf_text = ptr; 1080 *(ptr += len) = '\n'; 1081 STRCPY(++ptr, fields->errmsg); 1082 } 1083 if (qfprev->qf_nr == -1) 1084 qfprev->qf_nr = fields->enr; 1085 if (vim_isprintc(fields->type) && !qfprev->qf_type) 1086 /* only printable chars allowed */ 1087 qfprev->qf_type = fields->type; 1088 1089 if (!qfprev->qf_lnum) 1090 qfprev->qf_lnum = fields->lnum; 1091 if (!qfprev->qf_col) 1092 qfprev->qf_col = fields->col; 1093 qfprev->qf_viscol = fields->use_viscol; 1094 if (!qfprev->qf_fnum) 1095 qfprev->qf_fnum = qf_get_fnum(qi, qf_idx, 1096 qfl->qf_directory, 1097 *fields->namebuf || qfl->qf_directory != NULL 1098 ? fields->namebuf 1099 : qfl->qf_currfile != NULL && fields->valid 1100 ? qfl->qf_currfile : 0); 1101 } 1102 if (idx == 'Z') 1103 qfl->qf_multiline = qfl->qf_multiignore = FALSE; 1104 line_breakcheck(); 1105 return QF_IGNORE_LINE; 1106 } 1107 else if (vim_strchr((char_u *)"OPQ", idx) != NULL) 1108 { 1109 /* global file names */ 1110 fields->valid = FALSE; 1111 if (*fields->namebuf == NUL || mch_getperm(fields->namebuf) >= 0) 1112 { 1113 if (*fields->namebuf && idx == 'P') 1114 qfl->qf_currfile = 1115 qf_push_dir(fields->namebuf, &qfl->qf_file_stack, TRUE); 1116 else if (idx == 'Q') 1117 qfl->qf_currfile = qf_pop_dir(&qfl->qf_file_stack); 1118 *fields->namebuf = NUL; 1119 if (tail && *tail) 1120 { 1121 STRMOVE(IObuff, skipwhite(tail)); 1122 qfl->qf_multiscan = TRUE; 1123 goto restofline; 1124 } 1125 } 1126 } 1127 if (fmt_ptr->flags == '-') /* generally exclude this line */ 1128 { 1129 if (qfl->qf_multiline) 1130 /* also exclude continuation lines */ 1131 qfl->qf_multiignore = TRUE; 1132 return QF_IGNORE_LINE; 1133 } 1134 } 1135 1136 return QF_OK; 1137 } 1138 1139 /* 1140 * Read the errorfile "efile" into memory, line by line, building the error 1141 * list. 1142 * Alternative: when "efile" is NULL read errors from buffer "buf". 1143 * Alternative: when "tv" is not NULL get errors from the string or list. 1144 * Always use 'errorformat' from "buf" if there is a local value. 1145 * Then "lnumfirst" and "lnumlast" specify the range of lines to use. 1146 * Set the title of the list to "qf_title". 1147 * Return -1 for error, number of errors for success. 1148 */ 1149 static int 1150 qf_init_ext( 1151 qf_info_T *qi, 1152 int qf_idx, 1153 char_u *efile, 1154 buf_T *buf, 1155 typval_T *tv, 1156 char_u *errorformat, 1157 int newlist, /* TRUE: start a new error list */ 1158 linenr_T lnumfirst, /* first line number to use */ 1159 linenr_T lnumlast, /* last line number to use */ 1160 char_u *qf_title, 1161 char_u *enc) 1162 { 1163 qf_list_T *qfl; 1164 qfstate_T state; 1165 qffields_T fields; 1166 qfline_T *old_last = NULL; 1167 int adding = FALSE; 1168 static efm_T *fmt_first = NULL; 1169 char_u *efm; 1170 static char_u *last_efm = NULL; 1171 int retval = -1; /* default: return error flag */ 1172 int status; 1173 1174 /* Do not used the cached buffer, it may have been wiped out. */ 1175 vim_free(qf_last_bufname); 1176 qf_last_bufname = NULL; 1177 1178 vim_memset(&state, 0, sizeof(state)); 1179 vim_memset(&fields, 0, sizeof(fields)); 1180 #ifdef FEAT_MBYTE 1181 state.vc.vc_type = CONV_NONE; 1182 if (enc != NULL && *enc != NUL) 1183 convert_setup(&state.vc, enc, p_enc); 1184 #endif 1185 fields.namebuf = alloc_id(CMDBUFFSIZE + 1, aid_qf_namebuf); 1186 fields.errmsglen = CMDBUFFSIZE + 1; 1187 fields.errmsg = alloc_id(fields.errmsglen, aid_qf_errmsg); 1188 fields.pattern = alloc_id(CMDBUFFSIZE + 1, aid_qf_pattern); 1189 if (fields.namebuf == NULL || fields.errmsg == NULL || fields.pattern == NULL) 1190 goto qf_init_end; 1191 1192 if (efile != NULL && (state.fd = mch_fopen((char *)efile, "r")) == NULL) 1193 { 1194 EMSG2(_(e_openerrf), efile); 1195 goto qf_init_end; 1196 } 1197 1198 if (newlist || qf_idx == qi->qf_listcount) 1199 { 1200 /* make place for a new list */ 1201 qf_new_list(qi, qf_title); 1202 qf_idx = qi->qf_curlist; 1203 } 1204 else 1205 { 1206 /* Adding to existing list, use last entry. */ 1207 adding = TRUE; 1208 if (qi->qf_lists[qf_idx].qf_count > 0) 1209 old_last = qi->qf_lists[qf_idx].qf_last; 1210 } 1211 1212 qfl = &qi->qf_lists[qf_idx]; 1213 1214 /* Use the local value of 'errorformat' if it's set. */ 1215 if (errorformat == p_efm && tv == NULL && *buf->b_p_efm != NUL) 1216 efm = buf->b_p_efm; 1217 else 1218 efm = errorformat; 1219 1220 /* 1221 * If the errorformat didn't change between calls, then reuse the 1222 * previously parsed values. 1223 */ 1224 if (last_efm == NULL || (STRCMP(last_efm, efm) != 0)) 1225 { 1226 /* free the previously parsed data */ 1227 vim_free(last_efm); 1228 last_efm = NULL; 1229 free_efm_list(&fmt_first); 1230 1231 /* parse the current 'efm' */ 1232 fmt_first = parse_efm_option(efm); 1233 if (fmt_first != NULL) 1234 last_efm = vim_strsave(efm); 1235 } 1236 1237 if (fmt_first == NULL) /* nothing found */ 1238 goto error2; 1239 1240 /* 1241 * got_int is reset here, because it was probably set when killing the 1242 * ":make" command, but we still want to read the errorfile then. 1243 */ 1244 got_int = FALSE; 1245 1246 if (tv != NULL) 1247 { 1248 if (tv->v_type == VAR_STRING) 1249 state.p_str = tv->vval.v_string; 1250 else if (tv->v_type == VAR_LIST) 1251 state.p_li = tv->vval.v_list->lv_first; 1252 state.tv = tv; 1253 } 1254 state.buf = buf; 1255 state.buflnum = lnumfirst; 1256 state.lnumlast = lnumlast; 1257 1258 /* 1259 * Read the lines in the error file one by one. 1260 * Try to recognize one of the error formats in each line. 1261 */ 1262 while (!got_int) 1263 { 1264 /* Get the next line from a file/buffer/list/string */ 1265 status = qf_get_nextline(&state); 1266 if (status == QF_NOMEM) /* memory alloc failure */ 1267 goto qf_init_end; 1268 if (status == QF_END_OF_INPUT) /* end of input */ 1269 break; 1270 1271 status = qf_parse_line(qi, qf_idx, state.linebuf, state.linelen, 1272 fmt_first, &fields); 1273 if (status == QF_FAIL) 1274 goto error2; 1275 if (status == QF_NOMEM) 1276 goto qf_init_end; 1277 if (status == QF_IGNORE_LINE) 1278 continue; 1279 1280 if (qf_add_entry(qi, 1281 qf_idx, 1282 qfl->qf_directory, 1283 (*fields.namebuf || qfl->qf_directory != NULL) 1284 ? fields.namebuf 1285 : ((qfl->qf_currfile != NULL && fields.valid) 1286 ? qfl->qf_currfile : (char_u *)NULL), 1287 0, 1288 fields.errmsg, 1289 fields.lnum, 1290 fields.col, 1291 fields.use_viscol, 1292 fields.pattern, 1293 fields.enr, 1294 fields.type, 1295 fields.valid) == FAIL) 1296 goto error2; 1297 line_breakcheck(); 1298 } 1299 if (state.fd == NULL || !ferror(state.fd)) 1300 { 1301 if (qfl->qf_index == 0) 1302 { 1303 /* no valid entry found */ 1304 qfl->qf_ptr = qfl->qf_start; 1305 qfl->qf_index = 1; 1306 qfl->qf_nonevalid = TRUE; 1307 } 1308 else 1309 { 1310 qfl->qf_nonevalid = FALSE; 1311 if (qfl->qf_ptr == NULL) 1312 qfl->qf_ptr = qfl->qf_start; 1313 } 1314 /* return number of matches */ 1315 retval = qfl->qf_count; 1316 goto qf_init_end; 1317 } 1318 EMSG(_(e_readerrf)); 1319 error2: 1320 if (!adding) 1321 { 1322 /* Error when creating a new list. Free the new list */ 1323 qf_free(qi, qi->qf_curlist); 1324 qi->qf_listcount--; 1325 if (qi->qf_curlist > 0) 1326 --qi->qf_curlist; 1327 } 1328 qf_init_end: 1329 if (state.fd != NULL) 1330 fclose(state.fd); 1331 vim_free(fields.namebuf); 1332 vim_free(fields.errmsg); 1333 vim_free(fields.pattern); 1334 vim_free(state.growbuf); 1335 1336 if (qf_idx == qi->qf_curlist) 1337 qf_update_buffer(qi, old_last); 1338 #ifdef FEAT_MBYTE 1339 if (state.vc.vc_type != CONV_NONE) 1340 convert_setup(&state.vc, NULL, NULL); 1341 #endif 1342 1343 return retval; 1344 } 1345 1346 static void 1347 qf_store_title(qf_info_T *qi, int qf_idx, char_u *title) 1348 { 1349 vim_free(qi->qf_lists[qf_idx].qf_title); 1350 qi->qf_lists[qf_idx].qf_title = NULL; 1351 1352 if (title != NULL) 1353 { 1354 char_u *p = alloc((int)STRLEN(title) + 2); 1355 1356 qi->qf_lists[qf_idx].qf_title = p; 1357 if (p != NULL) 1358 sprintf((char *)p, ":%s", (char *)title); 1359 } 1360 } 1361 1362 /* 1363 * Prepare for adding a new quickfix list. If the current list is in the 1364 * middle of the stack, then all the following lists are freed and then 1365 * the new list is added. 1366 */ 1367 static void 1368 qf_new_list(qf_info_T *qi, char_u *qf_title) 1369 { 1370 int i; 1371 1372 /* 1373 * If the current entry is not the last entry, delete entries beyond 1374 * the current entry. This makes it possible to browse in a tree-like 1375 * way with ":grep'. 1376 */ 1377 while (qi->qf_listcount > qi->qf_curlist + 1) 1378 qf_free(qi, --qi->qf_listcount); 1379 1380 /* 1381 * When the stack is full, remove to oldest entry 1382 * Otherwise, add a new entry. 1383 */ 1384 if (qi->qf_listcount == LISTCOUNT) 1385 { 1386 qf_free(qi, 0); 1387 for (i = 1; i < LISTCOUNT; ++i) 1388 qi->qf_lists[i - 1] = qi->qf_lists[i]; 1389 qi->qf_curlist = LISTCOUNT - 1; 1390 } 1391 else 1392 qi->qf_curlist = qi->qf_listcount++; 1393 vim_memset(&qi->qf_lists[qi->qf_curlist], 0, (size_t)(sizeof(qf_list_T))); 1394 qf_store_title(qi, qi->qf_curlist, qf_title); 1395 qi->qf_lists[qi->qf_curlist].qf_id = ++last_qf_id; 1396 } 1397 1398 /* 1399 * Free a location list 1400 */ 1401 static void 1402 ll_free_all(qf_info_T **pqi) 1403 { 1404 int i; 1405 qf_info_T *qi; 1406 1407 qi = *pqi; 1408 if (qi == NULL) 1409 return; 1410 *pqi = NULL; /* Remove reference to this list */ 1411 1412 qi->qf_refcount--; 1413 if (qi->qf_refcount < 1) 1414 { 1415 /* No references to this location list */ 1416 for (i = 0; i < qi->qf_listcount; ++i) 1417 qf_free(qi, i); 1418 vim_free(qi); 1419 } 1420 } 1421 1422 void 1423 qf_free_all(win_T *wp) 1424 { 1425 int i; 1426 qf_info_T *qi = &ql_info; 1427 1428 if (wp != NULL) 1429 { 1430 /* location list */ 1431 ll_free_all(&wp->w_llist); 1432 ll_free_all(&wp->w_llist_ref); 1433 } 1434 else 1435 /* quickfix list */ 1436 for (i = 0; i < qi->qf_listcount; ++i) 1437 qf_free(qi, i); 1438 } 1439 1440 /* 1441 * Add an entry to the end of the list of errors. 1442 * Returns OK or FAIL. 1443 */ 1444 static int 1445 qf_add_entry( 1446 qf_info_T *qi, /* quickfix list */ 1447 int qf_idx, /* list index */ 1448 char_u *dir, /* optional directory name */ 1449 char_u *fname, /* file name or NULL */ 1450 int bufnum, /* buffer number or zero */ 1451 char_u *mesg, /* message */ 1452 long lnum, /* line number */ 1453 int col, /* column */ 1454 int vis_col, /* using visual column */ 1455 char_u *pattern, /* search pattern */ 1456 int nr, /* error number */ 1457 int type, /* type character */ 1458 int valid) /* valid entry */ 1459 { 1460 qfline_T *qfp; 1461 qfline_T **lastp; /* pointer to qf_last or NULL */ 1462 1463 if ((qfp = (qfline_T *)alloc((unsigned)sizeof(qfline_T))) == NULL) 1464 return FAIL; 1465 if (bufnum != 0) 1466 { 1467 buf_T *buf = buflist_findnr(bufnum); 1468 1469 qfp->qf_fnum = bufnum; 1470 if (buf != NULL) 1471 buf->b_has_qf_entry |= 1472 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY; 1473 } 1474 else 1475 qfp->qf_fnum = qf_get_fnum(qi, qf_idx, dir, fname); 1476 if ((qfp->qf_text = vim_strsave(mesg)) == NULL) 1477 { 1478 vim_free(qfp); 1479 return FAIL; 1480 } 1481 qfp->qf_lnum = lnum; 1482 qfp->qf_col = col; 1483 qfp->qf_viscol = vis_col; 1484 if (pattern == NULL || *pattern == NUL) 1485 qfp->qf_pattern = NULL; 1486 else if ((qfp->qf_pattern = vim_strsave(pattern)) == NULL) 1487 { 1488 vim_free(qfp->qf_text); 1489 vim_free(qfp); 1490 return FAIL; 1491 } 1492 qfp->qf_nr = nr; 1493 if (type != 1 && !vim_isprintc(type)) /* only printable chars allowed */ 1494 type = 0; 1495 qfp->qf_type = type; 1496 qfp->qf_valid = valid; 1497 1498 lastp = &qi->qf_lists[qf_idx].qf_last; 1499 if (qi->qf_lists[qf_idx].qf_count == 0) 1500 /* first element in the list */ 1501 { 1502 qi->qf_lists[qf_idx].qf_start = qfp; 1503 qi->qf_lists[qf_idx].qf_ptr = qfp; 1504 qi->qf_lists[qf_idx].qf_index = 0; 1505 qfp->qf_prev = NULL; 1506 } 1507 else 1508 { 1509 qfp->qf_prev = *lastp; 1510 (*lastp)->qf_next = qfp; 1511 } 1512 qfp->qf_next = NULL; 1513 qfp->qf_cleared = FALSE; 1514 *lastp = qfp; 1515 ++qi->qf_lists[qf_idx].qf_count; 1516 if (qi->qf_lists[qf_idx].qf_index == 0 && qfp->qf_valid) 1517 /* first valid entry */ 1518 { 1519 qi->qf_lists[qf_idx].qf_index = 1520 qi->qf_lists[qf_idx].qf_count; 1521 qi->qf_lists[qf_idx].qf_ptr = qfp; 1522 } 1523 1524 return OK; 1525 } 1526 1527 /* 1528 * Allocate a new location list 1529 */ 1530 static qf_info_T * 1531 ll_new_list(void) 1532 { 1533 qf_info_T *qi; 1534 1535 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T)); 1536 if (qi != NULL) 1537 { 1538 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T))); 1539 qi->qf_refcount++; 1540 } 1541 1542 return qi; 1543 } 1544 1545 /* 1546 * Return the location list for window 'wp'. 1547 * If not present, allocate a location list 1548 */ 1549 static qf_info_T * 1550 ll_get_or_alloc_list(win_T *wp) 1551 { 1552 if (IS_LL_WINDOW(wp)) 1553 /* For a location list window, use the referenced location list */ 1554 return wp->w_llist_ref; 1555 1556 /* 1557 * For a non-location list window, w_llist_ref should not point to a 1558 * location list. 1559 */ 1560 ll_free_all(&wp->w_llist_ref); 1561 1562 if (wp->w_llist == NULL) 1563 wp->w_llist = ll_new_list(); /* new location list */ 1564 return wp->w_llist; 1565 } 1566 1567 /* 1568 * Copy the location list from window "from" to window "to". 1569 */ 1570 void 1571 copy_loclist(win_T *from, win_T *to) 1572 { 1573 qf_info_T *qi; 1574 int idx; 1575 int i; 1576 1577 /* 1578 * When copying from a location list window, copy the referenced 1579 * location list. For other windows, copy the location list for 1580 * that window. 1581 */ 1582 if (IS_LL_WINDOW(from)) 1583 qi = from->w_llist_ref; 1584 else 1585 qi = from->w_llist; 1586 1587 if (qi == NULL) /* no location list to copy */ 1588 return; 1589 1590 /* allocate a new location list */ 1591 if ((to->w_llist = ll_new_list()) == NULL) 1592 return; 1593 1594 to->w_llist->qf_listcount = qi->qf_listcount; 1595 1596 /* Copy the location lists one at a time */ 1597 for (idx = 0; idx < qi->qf_listcount; idx++) 1598 { 1599 qf_list_T *from_qfl; 1600 qf_list_T *to_qfl; 1601 1602 to->w_llist->qf_curlist = idx; 1603 1604 from_qfl = &qi->qf_lists[idx]; 1605 to_qfl = &to->w_llist->qf_lists[idx]; 1606 1607 /* Some of the fields are populated by qf_add_entry() */ 1608 to_qfl->qf_nonevalid = from_qfl->qf_nonevalid; 1609 to_qfl->qf_count = 0; 1610 to_qfl->qf_index = 0; 1611 to_qfl->qf_start = NULL; 1612 to_qfl->qf_last = NULL; 1613 to_qfl->qf_ptr = NULL; 1614 if (from_qfl->qf_title != NULL) 1615 to_qfl->qf_title = vim_strsave(from_qfl->qf_title); 1616 else 1617 to_qfl->qf_title = NULL; 1618 if (from_qfl->qf_ctx != NULL) 1619 { 1620 to_qfl->qf_ctx = alloc_tv(); 1621 if (to_qfl->qf_ctx != NULL) 1622 copy_tv(from_qfl->qf_ctx, to_qfl->qf_ctx); 1623 } 1624 else 1625 to_qfl->qf_ctx = NULL; 1626 1627 if (from_qfl->qf_count) 1628 { 1629 qfline_T *from_qfp; 1630 qfline_T *prevp; 1631 1632 /* copy all the location entries in this list */ 1633 for (i = 0, from_qfp = from_qfl->qf_start; 1634 i < from_qfl->qf_count && from_qfp != NULL; 1635 ++i, from_qfp = from_qfp->qf_next) 1636 { 1637 if (qf_add_entry(to->w_llist, 1638 to->w_llist->qf_curlist, 1639 NULL, 1640 NULL, 1641 0, 1642 from_qfp->qf_text, 1643 from_qfp->qf_lnum, 1644 from_qfp->qf_col, 1645 from_qfp->qf_viscol, 1646 from_qfp->qf_pattern, 1647 from_qfp->qf_nr, 1648 0, 1649 from_qfp->qf_valid) == FAIL) 1650 { 1651 qf_free_all(to); 1652 return; 1653 } 1654 /* 1655 * qf_add_entry() will not set the qf_num field, as the 1656 * directory and file names are not supplied. So the qf_fnum 1657 * field is copied here. 1658 */ 1659 prevp = to->w_llist->qf_lists[to->w_llist->qf_curlist].qf_last; 1660 prevp->qf_fnum = from_qfp->qf_fnum; /* file number */ 1661 prevp->qf_type = from_qfp->qf_type; /* error type */ 1662 if (from_qfl->qf_ptr == from_qfp) 1663 to_qfl->qf_ptr = prevp; /* current location */ 1664 } 1665 } 1666 1667 to_qfl->qf_index = from_qfl->qf_index; /* current index in the list */ 1668 1669 /* Assign a new ID for the location list */ 1670 to_qfl->qf_id = ++last_qf_id; 1671 1672 /* When no valid entries are present in the list, qf_ptr points to 1673 * the first item in the list */ 1674 if (to_qfl->qf_nonevalid) 1675 { 1676 to_qfl->qf_ptr = to_qfl->qf_start; 1677 to_qfl->qf_index = 1; 1678 } 1679 } 1680 1681 to->w_llist->qf_curlist = qi->qf_curlist; /* current list */ 1682 } 1683 1684 /* 1685 * Get buffer number for file "directory/fname". 1686 * Also sets the b_has_qf_entry flag. 1687 */ 1688 static int 1689 qf_get_fnum(qf_info_T *qi, int qf_idx, char_u *directory, char_u *fname) 1690 { 1691 char_u *ptr = NULL; 1692 buf_T *buf; 1693 char_u *bufname; 1694 1695 if (fname == NULL || *fname == NUL) /* no file name */ 1696 return 0; 1697 1698 #ifdef VMS 1699 vms_remove_version(fname); 1700 #endif 1701 #ifdef BACKSLASH_IN_FILENAME 1702 if (directory != NULL) 1703 slash_adjust(directory); 1704 slash_adjust(fname); 1705 #endif 1706 if (directory != NULL && !vim_isAbsName(fname) 1707 && (ptr = concat_fnames(directory, fname, TRUE)) != NULL) 1708 { 1709 /* 1710 * Here we check if the file really exists. 1711 * This should normally be true, but if make works without 1712 * "leaving directory"-messages we might have missed a 1713 * directory change. 1714 */ 1715 if (mch_getperm(ptr) < 0) 1716 { 1717 vim_free(ptr); 1718 directory = qf_guess_filepath(qi, qf_idx, fname); 1719 if (directory) 1720 ptr = concat_fnames(directory, fname, TRUE); 1721 else 1722 ptr = vim_strsave(fname); 1723 } 1724 /* Use concatenated directory name and file name */ 1725 bufname = ptr; 1726 } 1727 else 1728 bufname = fname; 1729 1730 if (qf_last_bufname != NULL && STRCMP(bufname, qf_last_bufname) == 0 1731 && bufref_valid(&qf_last_bufref)) 1732 { 1733 buf = qf_last_bufref.br_buf; 1734 vim_free(ptr); 1735 } 1736 else 1737 { 1738 vim_free(qf_last_bufname); 1739 buf = buflist_new(bufname, NULL, (linenr_T)0, BLN_NOOPT); 1740 if (bufname == ptr) 1741 qf_last_bufname = bufname; 1742 else 1743 qf_last_bufname = vim_strsave(bufname); 1744 set_bufref(&qf_last_bufref, buf); 1745 } 1746 if (buf == NULL) 1747 return 0; 1748 1749 buf->b_has_qf_entry = 1750 (qi == &ql_info) ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY; 1751 return buf->b_fnum; 1752 } 1753 1754 /* 1755 * Push dirbuf onto the directory stack and return pointer to actual dir or 1756 * NULL on error. 1757 */ 1758 static char_u * 1759 qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr, int is_file_stack) 1760 { 1761 struct dir_stack_T *ds_new; 1762 struct dir_stack_T *ds_ptr; 1763 1764 /* allocate new stack element and hook it in */ 1765 ds_new = (struct dir_stack_T *)alloc((unsigned)sizeof(struct dir_stack_T)); 1766 if (ds_new == NULL) 1767 return NULL; 1768 1769 ds_new->next = *stackptr; 1770 *stackptr = ds_new; 1771 1772 /* store directory on the stack */ 1773 if (vim_isAbsName(dirbuf) 1774 || (*stackptr)->next == NULL 1775 || (*stackptr && is_file_stack)) 1776 (*stackptr)->dirname = vim_strsave(dirbuf); 1777 else 1778 { 1779 /* Okay we don't have an absolute path. 1780 * dirbuf must be a subdir of one of the directories on the stack. 1781 * Let's search... 1782 */ 1783 ds_new = (*stackptr)->next; 1784 (*stackptr)->dirname = NULL; 1785 while (ds_new) 1786 { 1787 vim_free((*stackptr)->dirname); 1788 (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf, 1789 TRUE); 1790 if (mch_isdir((*stackptr)->dirname) == TRUE) 1791 break; 1792 1793 ds_new = ds_new->next; 1794 } 1795 1796 /* clean up all dirs we already left */ 1797 while ((*stackptr)->next != ds_new) 1798 { 1799 ds_ptr = (*stackptr)->next; 1800 (*stackptr)->next = (*stackptr)->next->next; 1801 vim_free(ds_ptr->dirname); 1802 vim_free(ds_ptr); 1803 } 1804 1805 /* Nothing found -> it must be on top level */ 1806 if (ds_new == NULL) 1807 { 1808 vim_free((*stackptr)->dirname); 1809 (*stackptr)->dirname = vim_strsave(dirbuf); 1810 } 1811 } 1812 1813 if ((*stackptr)->dirname != NULL) 1814 return (*stackptr)->dirname; 1815 else 1816 { 1817 ds_ptr = *stackptr; 1818 *stackptr = (*stackptr)->next; 1819 vim_free(ds_ptr); 1820 return NULL; 1821 } 1822 } 1823 1824 1825 /* 1826 * pop dirbuf from the directory stack and return previous directory or NULL if 1827 * stack is empty 1828 */ 1829 static char_u * 1830 qf_pop_dir(struct dir_stack_T **stackptr) 1831 { 1832 struct dir_stack_T *ds_ptr; 1833 1834 /* TODO: Should we check if dirbuf is the directory on top of the stack? 1835 * What to do if it isn't? */ 1836 1837 /* pop top element and free it */ 1838 if (*stackptr != NULL) 1839 { 1840 ds_ptr = *stackptr; 1841 *stackptr = (*stackptr)->next; 1842 vim_free(ds_ptr->dirname); 1843 vim_free(ds_ptr); 1844 } 1845 1846 /* return NEW top element as current dir or NULL if stack is empty*/ 1847 return *stackptr ? (*stackptr)->dirname : NULL; 1848 } 1849 1850 /* 1851 * clean up directory stack 1852 */ 1853 static void 1854 qf_clean_dir_stack(struct dir_stack_T **stackptr) 1855 { 1856 struct dir_stack_T *ds_ptr; 1857 1858 while ((ds_ptr = *stackptr) != NULL) 1859 { 1860 *stackptr = (*stackptr)->next; 1861 vim_free(ds_ptr->dirname); 1862 vim_free(ds_ptr); 1863 } 1864 } 1865 1866 /* 1867 * Check in which directory of the directory stack the given file can be 1868 * found. 1869 * Returns a pointer to the directory name or NULL if not found. 1870 * Cleans up intermediate directory entries. 1871 * 1872 * TODO: How to solve the following problem? 1873 * If we have the this directory tree: 1874 * ./ 1875 * ./aa 1876 * ./aa/bb 1877 * ./bb 1878 * ./bb/x.c 1879 * and make says: 1880 * making all in aa 1881 * making all in bb 1882 * x.c:9: Error 1883 * Then qf_push_dir thinks we are in ./aa/bb, but we are in ./bb. 1884 * qf_guess_filepath will return NULL. 1885 */ 1886 static char_u * 1887 qf_guess_filepath(qf_info_T *qi, int qf_idx, char_u *filename) 1888 { 1889 struct dir_stack_T *ds_ptr; 1890 struct dir_stack_T *ds_tmp; 1891 char_u *fullname; 1892 qf_list_T *qfl = &qi->qf_lists[qf_idx]; 1893 1894 /* no dirs on the stack - there's nothing we can do */ 1895 if (qfl->qf_dir_stack == NULL) 1896 return NULL; 1897 1898 ds_ptr = qfl->qf_dir_stack->next; 1899 fullname = NULL; 1900 while (ds_ptr) 1901 { 1902 vim_free(fullname); 1903 fullname = concat_fnames(ds_ptr->dirname, filename, TRUE); 1904 1905 /* If concat_fnames failed, just go on. The worst thing that can happen 1906 * is that we delete the entire stack. 1907 */ 1908 if ((fullname != NULL) && (mch_getperm(fullname) >= 0)) 1909 break; 1910 1911 ds_ptr = ds_ptr->next; 1912 } 1913 1914 vim_free(fullname); 1915 1916 /* clean up all dirs we already left */ 1917 while (qfl->qf_dir_stack->next != ds_ptr) 1918 { 1919 ds_tmp = qfl->qf_dir_stack->next; 1920 qfl->qf_dir_stack->next = qfl->qf_dir_stack->next->next; 1921 vim_free(ds_tmp->dirname); 1922 vim_free(ds_tmp); 1923 } 1924 1925 return ds_ptr==NULL? NULL: ds_ptr->dirname; 1926 } 1927 1928 /* 1929 * When loading a file from the quickfix, the auto commands may modify it. 1930 * This may invalidate the current quickfix entry. This function checks 1931 * whether a entry is still present in the quickfix. 1932 * Similar to location list. 1933 */ 1934 static int 1935 is_qf_entry_present(qf_info_T *qi, qfline_T *qf_ptr) 1936 { 1937 qf_list_T *qfl; 1938 qfline_T *qfp; 1939 int i; 1940 1941 qfl = &qi->qf_lists[qi->qf_curlist]; 1942 1943 /* Search for the entry in the current list */ 1944 for (i = 0, qfp = qfl->qf_start; i < qfl->qf_count; 1945 ++i, qfp = qfp->qf_next) 1946 if (qfp == NULL || qfp == qf_ptr) 1947 break; 1948 1949 if (i == qfl->qf_count) /* Entry is not found */ 1950 return FALSE; 1951 1952 return TRUE; 1953 } 1954 1955 /* 1956 * Get the next valid entry in the current quickfix/location list. The search 1957 * starts from the current entry. Returns NULL on failure. 1958 */ 1959 static qfline_T * 1960 get_next_valid_entry( 1961 qf_info_T *qi, 1962 qfline_T *qf_ptr, 1963 int *qf_index, 1964 int dir) 1965 { 1966 int idx; 1967 int old_qf_fnum; 1968 1969 idx = *qf_index; 1970 old_qf_fnum = qf_ptr->qf_fnum; 1971 1972 do 1973 { 1974 if (idx == qi->qf_lists[qi->qf_curlist].qf_count 1975 || qf_ptr->qf_next == NULL) 1976 return NULL; 1977 ++idx; 1978 qf_ptr = qf_ptr->qf_next; 1979 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid 1980 && !qf_ptr->qf_valid) 1981 || (dir == FORWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); 1982 1983 *qf_index = idx; 1984 return qf_ptr; 1985 } 1986 1987 /* 1988 * Get the previous valid entry in the current quickfix/location list. The 1989 * search starts from the current entry. Returns NULL on failure. 1990 */ 1991 static qfline_T * 1992 get_prev_valid_entry( 1993 qf_info_T *qi, 1994 qfline_T *qf_ptr, 1995 int *qf_index, 1996 int dir) 1997 { 1998 int idx; 1999 int old_qf_fnum; 2000 2001 idx = *qf_index; 2002 old_qf_fnum = qf_ptr->qf_fnum; 2003 2004 do 2005 { 2006 if (idx == 1 || qf_ptr->qf_prev == NULL) 2007 return NULL; 2008 --idx; 2009 qf_ptr = qf_ptr->qf_prev; 2010 } while ((!qi->qf_lists[qi->qf_curlist].qf_nonevalid 2011 && !qf_ptr->qf_valid) 2012 || (dir == BACKWARD_FILE && qf_ptr->qf_fnum == old_qf_fnum)); 2013 2014 *qf_index = idx; 2015 return qf_ptr; 2016 } 2017 2018 /* 2019 * Get the n'th (errornr) previous/next valid entry from the current entry in 2020 * the quickfix list. 2021 * dir == FORWARD or FORWARD_FILE: next valid entry 2022 * dir == BACKWARD or BACKWARD_FILE: previous valid entry 2023 */ 2024 static qfline_T * 2025 get_nth_valid_entry( 2026 qf_info_T *qi, 2027 int errornr, 2028 qfline_T *qf_ptr, 2029 int *qf_index, 2030 int dir) 2031 { 2032 qfline_T *prev_qf_ptr; 2033 int prev_index; 2034 static char_u *e_no_more_items = (char_u *)N_("E553: No more items"); 2035 char_u *err = e_no_more_items; 2036 2037 while (errornr--) 2038 { 2039 prev_qf_ptr = qf_ptr; 2040 prev_index = *qf_index; 2041 2042 if (dir == FORWARD || dir == FORWARD_FILE) 2043 qf_ptr = get_next_valid_entry(qi, qf_ptr, qf_index, dir); 2044 else 2045 qf_ptr = get_prev_valid_entry(qi, qf_ptr, qf_index, dir); 2046 if (qf_ptr == NULL) 2047 { 2048 qf_ptr = prev_qf_ptr; 2049 *qf_index = prev_index; 2050 if (err != NULL) 2051 { 2052 EMSG(_(err)); 2053 return NULL; 2054 } 2055 break; 2056 } 2057 2058 err = NULL; 2059 } 2060 2061 return qf_ptr; 2062 } 2063 2064 /* 2065 * Get n'th (errornr) quickfix entry 2066 */ 2067 static qfline_T * 2068 get_nth_entry( 2069 qf_info_T *qi, 2070 int errornr, 2071 qfline_T *qf_ptr, 2072 int *cur_qfidx) 2073 { 2074 int qf_idx = *cur_qfidx; 2075 2076 /* New error number is less than the current error number */ 2077 while (errornr < qf_idx && qf_idx > 1 && qf_ptr->qf_prev != NULL) 2078 { 2079 --qf_idx; 2080 qf_ptr = qf_ptr->qf_prev; 2081 } 2082 /* New error number is greater than the current error number */ 2083 while (errornr > qf_idx && 2084 qf_idx < qi->qf_lists[qi->qf_curlist].qf_count && 2085 qf_ptr->qf_next != NULL) 2086 { 2087 ++qf_idx; 2088 qf_ptr = qf_ptr->qf_next; 2089 } 2090 2091 *cur_qfidx = qf_idx; 2092 return qf_ptr; 2093 } 2094 2095 /* 2096 * Find a help window or open one. 2097 */ 2098 static int 2099 jump_to_help_window(qf_info_T *qi, int *opened_window) 2100 { 2101 win_T *wp; 2102 int flags; 2103 2104 if (cmdmod.tab != 0) 2105 wp = NULL; 2106 else 2107 FOR_ALL_WINDOWS(wp) 2108 if (bt_help(wp->w_buffer)) 2109 break; 2110 if (wp != NULL && wp->w_buffer->b_nwindows > 0) 2111 win_enter(wp, TRUE); 2112 else 2113 { 2114 /* 2115 * Split off help window; put it at far top if no position 2116 * specified, the current window is vertically split and narrow. 2117 */ 2118 flags = WSP_HELP; 2119 if (cmdmod.split == 0 && curwin->w_width != Columns 2120 && curwin->w_width < 80) 2121 flags |= WSP_TOP; 2122 if (qi != &ql_info) 2123 flags |= WSP_NEWLOC; /* don't copy the location list */ 2124 2125 if (win_split(0, flags) == FAIL) 2126 return FAIL; 2127 2128 *opened_window = TRUE; 2129 2130 if (curwin->w_height < p_hh) 2131 win_setheight((int)p_hh); 2132 2133 if (qi != &ql_info) /* not a quickfix list */ 2134 { 2135 /* The new window should use the supplied location list */ 2136 curwin->w_llist = qi; 2137 qi->qf_refcount++; 2138 } 2139 } 2140 2141 if (!p_im) 2142 restart_edit = 0; /* don't want insert mode in help file */ 2143 2144 return OK; 2145 } 2146 2147 /* 2148 * Find a suitable window for opening a file (qf_fnum) and jump to it. 2149 * If the file is already opened in a window, jump to it. 2150 */ 2151 static int 2152 qf_jump_to_usable_window(int qf_fnum, int *opened_window) 2153 { 2154 win_T *usable_win_ptr = NULL; 2155 int usable_win; 2156 qf_info_T *ll_ref; 2157 int flags; 2158 win_T *win; 2159 win_T *altwin; 2160 2161 usable_win = 0; 2162 2163 ll_ref = curwin->w_llist_ref; 2164 if (ll_ref != NULL) 2165 { 2166 /* Find a window using the same location list that is not a 2167 * quickfix window. */ 2168 FOR_ALL_WINDOWS(usable_win_ptr) 2169 if (usable_win_ptr->w_llist == ll_ref 2170 && !bt_quickfix(usable_win_ptr->w_buffer)) 2171 { 2172 usable_win = 1; 2173 break; 2174 } 2175 } 2176 2177 if (!usable_win) 2178 { 2179 /* Locate a window showing a normal buffer */ 2180 FOR_ALL_WINDOWS(win) 2181 if (win->w_buffer->b_p_bt[0] == NUL) 2182 { 2183 usable_win = 1; 2184 break; 2185 } 2186 } 2187 2188 /* 2189 * If no usable window is found and 'switchbuf' contains "usetab" 2190 * then search in other tabs. 2191 */ 2192 if (!usable_win && (swb_flags & SWB_USETAB)) 2193 { 2194 tabpage_T *tp; 2195 win_T *wp; 2196 2197 FOR_ALL_TAB_WINDOWS(tp, wp) 2198 { 2199 if (wp->w_buffer->b_fnum == qf_fnum) 2200 { 2201 goto_tabpage_win(tp, wp); 2202 usable_win = 1; 2203 goto win_found; 2204 } 2205 } 2206 } 2207 win_found: 2208 2209 /* 2210 * If there is only one window and it is the quickfix window, create a 2211 * new one above the quickfix window. 2212 */ 2213 if ((ONE_WINDOW && bt_quickfix(curbuf)) || !usable_win) 2214 { 2215 flags = WSP_ABOVE; 2216 if (ll_ref != NULL) 2217 flags |= WSP_NEWLOC; 2218 if (win_split(0, flags) == FAIL) 2219 return FAIL; /* not enough room for window */ 2220 *opened_window = TRUE; /* close it when fail */ 2221 p_swb = empty_option; /* don't split again */ 2222 swb_flags = 0; 2223 RESET_BINDING(curwin); 2224 if (ll_ref != NULL) 2225 { 2226 /* The new window should use the location list from the 2227 * location list window */ 2228 curwin->w_llist = ll_ref; 2229 ll_ref->qf_refcount++; 2230 } 2231 } 2232 else 2233 { 2234 if (curwin->w_llist_ref != NULL) 2235 { 2236 /* In a location window */ 2237 win = usable_win_ptr; 2238 if (win == NULL) 2239 { 2240 /* Find the window showing the selected file */ 2241 FOR_ALL_WINDOWS(win) 2242 if (win->w_buffer->b_fnum == qf_fnum) 2243 break; 2244 if (win == NULL) 2245 { 2246 /* Find a previous usable window */ 2247 win = curwin; 2248 do 2249 { 2250 if (win->w_buffer->b_p_bt[0] == NUL) 2251 break; 2252 if (win->w_prev == NULL) 2253 win = lastwin; /* wrap around the top */ 2254 else 2255 win = win->w_prev; /* go to previous window */ 2256 } while (win != curwin); 2257 } 2258 } 2259 win_goto(win); 2260 2261 /* If the location list for the window is not set, then set it 2262 * to the location list from the location window */ 2263 if (win->w_llist == NULL) 2264 { 2265 win->w_llist = ll_ref; 2266 ll_ref->qf_refcount++; 2267 } 2268 } 2269 else 2270 { 2271 2272 /* 2273 * Try to find a window that shows the right buffer. 2274 * Default to the window just above the quickfix buffer. 2275 */ 2276 win = curwin; 2277 altwin = NULL; 2278 for (;;) 2279 { 2280 if (win->w_buffer->b_fnum == qf_fnum) 2281 break; 2282 if (win->w_prev == NULL) 2283 win = lastwin; /* wrap around the top */ 2284 else 2285 win = win->w_prev; /* go to previous window */ 2286 2287 if (IS_QF_WINDOW(win)) 2288 { 2289 /* Didn't find it, go to the window before the quickfix 2290 * window. */ 2291 if (altwin != NULL) 2292 win = altwin; 2293 else if (curwin->w_prev != NULL) 2294 win = curwin->w_prev; 2295 else 2296 win = curwin->w_next; 2297 break; 2298 } 2299 2300 /* Remember a usable window. */ 2301 if (altwin == NULL && !win->w_p_pvw 2302 && win->w_buffer->b_p_bt[0] == NUL) 2303 altwin = win; 2304 } 2305 2306 win_goto(win); 2307 } 2308 } 2309 2310 return OK; 2311 } 2312 2313 /* 2314 * Edit the selected file or help file. 2315 */ 2316 static int 2317 qf_jump_edit_buffer( 2318 qf_info_T *qi, 2319 qfline_T *qf_ptr, 2320 int forceit, 2321 win_T *oldwin, 2322 int *opened_window, 2323 int *abort) 2324 { 2325 int retval = OK; 2326 2327 if (qf_ptr->qf_type == 1) 2328 { 2329 /* Open help file (do_ecmd() will set b_help flag, readfile() will 2330 * set b_p_ro flag). */ 2331 if (!can_abandon(curbuf, forceit)) 2332 { 2333 no_write_message(); 2334 retval = FALSE; 2335 } 2336 else 2337 retval = do_ecmd(qf_ptr->qf_fnum, NULL, NULL, NULL, (linenr_T)1, 2338 ECMD_HIDE + ECMD_SET_HELP, 2339 oldwin == curwin ? curwin : NULL); 2340 } 2341 else 2342 { 2343 int old_qf_curlist = qi->qf_curlist; 2344 2345 retval = buflist_getfile(qf_ptr->qf_fnum, 2346 (linenr_T)1, GETF_SETMARK | GETF_SWITCH, forceit); 2347 if (qi != &ql_info && !win_valid_any_tab(oldwin)) 2348 { 2349 EMSG(_("E924: Current window was closed")); 2350 *abort = TRUE; 2351 *opened_window = FALSE; 2352 } 2353 else if (old_qf_curlist != qi->qf_curlist 2354 || !is_qf_entry_present(qi, qf_ptr)) 2355 { 2356 if (qi == &ql_info) 2357 EMSG(_("E925: Current quickfix was changed")); 2358 else 2359 EMSG(_("E926: Current location list was changed")); 2360 *abort = TRUE; 2361 } 2362 2363 if (*abort) 2364 retval = FALSE; 2365 } 2366 2367 return retval; 2368 } 2369 2370 /* 2371 * Goto the error line in the current file using either line/column number or a 2372 * search pattern. 2373 */ 2374 static void 2375 qf_jump_goto_line( 2376 linenr_T qf_lnum, 2377 int qf_col, 2378 char_u qf_viscol, 2379 char_u *qf_pattern) 2380 { 2381 linenr_T i; 2382 char_u *line; 2383 colnr_T screen_col; 2384 colnr_T char_col; 2385 2386 if (qf_pattern == NULL) 2387 { 2388 /* 2389 * Go to line with error, unless qf_lnum is 0. 2390 */ 2391 i = qf_lnum; 2392 if (i > 0) 2393 { 2394 if (i > curbuf->b_ml.ml_line_count) 2395 i = curbuf->b_ml.ml_line_count; 2396 curwin->w_cursor.lnum = i; 2397 } 2398 if (qf_col > 0) 2399 { 2400 curwin->w_cursor.col = qf_col - 1; 2401 #ifdef FEAT_VIRTUALEDIT 2402 curwin->w_cursor.coladd = 0; 2403 #endif 2404 if (qf_viscol == TRUE) 2405 { 2406 /* 2407 * Check each character from the beginning of the error 2408 * line up to the error column. For each tab character 2409 * found, reduce the error column value by the length of 2410 * a tab character. 2411 */ 2412 line = ml_get_curline(); 2413 screen_col = 0; 2414 for (char_col = 0; char_col < curwin->w_cursor.col; ++char_col) 2415 { 2416 if (*line == NUL) 2417 break; 2418 if (*line++ == '\t') 2419 { 2420 curwin->w_cursor.col -= 7 - (screen_col % 8); 2421 screen_col += 8 - (screen_col % 8); 2422 } 2423 else 2424 ++screen_col; 2425 } 2426 } 2427 check_cursor(); 2428 } 2429 else 2430 beginline(BL_WHITE | BL_FIX); 2431 } 2432 else 2433 { 2434 pos_T save_cursor; 2435 2436 /* Move the cursor to the first line in the buffer */ 2437 save_cursor = curwin->w_cursor; 2438 curwin->w_cursor.lnum = 0; 2439 if (!do_search(NULL, '/', qf_pattern, (long)1, 2440 SEARCH_KEEP, NULL, NULL)) 2441 curwin->w_cursor = save_cursor; 2442 } 2443 } 2444 2445 /* 2446 * Display quickfix list index and size message 2447 */ 2448 static void 2449 qf_jump_print_msg( 2450 qf_info_T *qi, 2451 int qf_index, 2452 qfline_T *qf_ptr, 2453 buf_T *old_curbuf, 2454 linenr_T old_lnum) 2455 { 2456 linenr_T i; 2457 int len; 2458 2459 /* Update the screen before showing the message, unless the screen 2460 * scrolled up. */ 2461 if (!msg_scrolled) 2462 update_topline_redraw(); 2463 sprintf((char *)IObuff, _("(%d of %d)%s%s: "), qf_index, 2464 qi->qf_lists[qi->qf_curlist].qf_count, 2465 qf_ptr->qf_cleared ? _(" (line deleted)") : "", 2466 (char *)qf_types(qf_ptr->qf_type, qf_ptr->qf_nr)); 2467 /* Add the message, skipping leading whitespace and newlines. */ 2468 len = (int)STRLEN(IObuff); 2469 qf_fmt_text(skipwhite(qf_ptr->qf_text), IObuff + len, IOSIZE - len); 2470 2471 /* Output the message. Overwrite to avoid scrolling when the 'O' 2472 * flag is present in 'shortmess'; But when not jumping, print the 2473 * whole message. */ 2474 i = msg_scroll; 2475 if (curbuf == old_curbuf && curwin->w_cursor.lnum == old_lnum) 2476 msg_scroll = TRUE; 2477 else if (!msg_scrolled && shortmess(SHM_OVERALL)) 2478 msg_scroll = FALSE; 2479 msg_attr_keep(IObuff, 0, TRUE); 2480 msg_scroll = i; 2481 } 2482 2483 /* 2484 * jump to a quickfix line 2485 * if dir == FORWARD go "errornr" valid entries forward 2486 * if dir == BACKWARD go "errornr" valid entries backward 2487 * if dir == FORWARD_FILE go "errornr" valid entries files backward 2488 * if dir == BACKWARD_FILE go "errornr" valid entries files backward 2489 * else if "errornr" is zero, redisplay the same line 2490 * else go to entry "errornr" 2491 */ 2492 void 2493 qf_jump(qf_info_T *qi, 2494 int dir, 2495 int errornr, 2496 int forceit) 2497 { 2498 qfline_T *qf_ptr; 2499 qfline_T *old_qf_ptr; 2500 int qf_index; 2501 int old_qf_index; 2502 buf_T *old_curbuf; 2503 linenr_T old_lnum; 2504 char_u *old_swb = p_swb; 2505 unsigned old_swb_flags = swb_flags; 2506 int opened_window = FALSE; 2507 win_T *oldwin = curwin; 2508 int print_message = TRUE; 2509 #ifdef FEAT_FOLDING 2510 int old_KeyTyped = KeyTyped; /* getting file may reset it */ 2511 #endif 2512 int retval = OK; 2513 2514 if (qi == NULL) 2515 qi = &ql_info; 2516 2517 if (qi->qf_curlist >= qi->qf_listcount 2518 || qi->qf_lists[qi->qf_curlist].qf_count == 0) 2519 { 2520 EMSG(_(e_quickfix)); 2521 return; 2522 } 2523 2524 qf_ptr = qi->qf_lists[qi->qf_curlist].qf_ptr; 2525 old_qf_ptr = qf_ptr; 2526 qf_index = qi->qf_lists[qi->qf_curlist].qf_index; 2527 old_qf_index = qf_index; 2528 if (dir != 0) /* next/prev valid entry */ 2529 { 2530 qf_ptr = get_nth_valid_entry(qi, errornr, qf_ptr, &qf_index, dir); 2531 if (qf_ptr == NULL) 2532 { 2533 qf_ptr = old_qf_ptr; 2534 qf_index = old_qf_index; 2535 goto theend; 2536 } 2537 } 2538 else if (errornr != 0) /* go to specified number */ 2539 qf_ptr = get_nth_entry(qi, errornr, qf_ptr, &qf_index); 2540 2541 qi->qf_lists[qi->qf_curlist].qf_index = qf_index; 2542 if (qf_win_pos_update(qi, old_qf_index)) 2543 /* No need to print the error message if it's visible in the error 2544 * window */ 2545 print_message = FALSE; 2546 2547 /* 2548 * For ":helpgrep" find a help window or open one. 2549 */ 2550 if (qf_ptr->qf_type == 1 && (!bt_help(curwin->w_buffer) || cmdmod.tab != 0)) 2551 if (jump_to_help_window(qi, &opened_window) == FAIL) 2552 goto theend; 2553 2554 /* 2555 * If currently in the quickfix window, find another window to show the 2556 * file in. 2557 */ 2558 if (bt_quickfix(curbuf) && !opened_window) 2559 { 2560 /* 2561 * If there is no file specified, we don't know where to go. 2562 * But do advance, otherwise ":cn" gets stuck. 2563 */ 2564 if (qf_ptr->qf_fnum == 0) 2565 goto theend; 2566 2567 if (qf_jump_to_usable_window(qf_ptr->qf_fnum, &opened_window) == FAIL) 2568 goto failed; 2569 } 2570 2571 /* 2572 * If there is a file name, 2573 * read the wanted file if needed, and check autowrite etc. 2574 */ 2575 old_curbuf = curbuf; 2576 old_lnum = curwin->w_cursor.lnum; 2577 2578 if (qf_ptr->qf_fnum != 0) 2579 { 2580 int abort = FALSE; 2581 2582 retval = qf_jump_edit_buffer(qi, qf_ptr, forceit, oldwin, 2583 &opened_window, &abort); 2584 if (abort) 2585 { 2586 qi = NULL; 2587 qf_ptr = NULL; 2588 } 2589 } 2590 2591 if (retval == OK) 2592 { 2593 /* When not switched to another buffer, still need to set pc mark */ 2594 if (curbuf == old_curbuf) 2595 setpcmark(); 2596 2597 qf_jump_goto_line(qf_ptr->qf_lnum, qf_ptr->qf_col, qf_ptr->qf_viscol, 2598 qf_ptr->qf_pattern); 2599 2600 #ifdef FEAT_FOLDING 2601 if ((fdo_flags & FDO_QUICKFIX) && old_KeyTyped) 2602 foldOpenCursor(); 2603 #endif 2604 if (print_message) 2605 qf_jump_print_msg(qi, qf_index, qf_ptr, old_curbuf, old_lnum); 2606 } 2607 else 2608 { 2609 if (opened_window) 2610 win_close(curwin, TRUE); /* Close opened window */ 2611 if (qf_ptr != NULL && qf_ptr->qf_fnum != 0) 2612 { 2613 /* 2614 * Couldn't open file, so put index back where it was. This could 2615 * happen if the file was readonly and we changed something. 2616 */ 2617 failed: 2618 qf_ptr = old_qf_ptr; 2619 qf_index = old_qf_index; 2620 } 2621 } 2622 theend: 2623 if (qi != NULL) 2624 { 2625 qi->qf_lists[qi->qf_curlist].qf_ptr = qf_ptr; 2626 qi->qf_lists[qi->qf_curlist].qf_index = qf_index; 2627 } 2628 if (p_swb != old_swb && opened_window) 2629 { 2630 /* Restore old 'switchbuf' value, but not when an autocommand or 2631 * modeline has changed the value. */ 2632 if (p_swb == empty_option) 2633 { 2634 p_swb = old_swb; 2635 swb_flags = old_swb_flags; 2636 } 2637 else 2638 free_string_option(old_swb); 2639 } 2640 } 2641 2642 /* 2643 * ":clist": list all errors 2644 * ":llist": list all locations 2645 */ 2646 void 2647 qf_list(exarg_T *eap) 2648 { 2649 buf_T *buf; 2650 char_u *fname; 2651 qfline_T *qfp; 2652 int i; 2653 int idx1 = 1; 2654 int idx2 = -1; 2655 char_u *arg = eap->arg; 2656 int plus = FALSE; 2657 int all = eap->forceit; /* if not :cl!, only show 2658 recognised errors */ 2659 qf_info_T *qi = &ql_info; 2660 2661 if (eap->cmdidx == CMD_llist) 2662 { 2663 qi = GET_LOC_LIST(curwin); 2664 if (qi == NULL) 2665 { 2666 EMSG(_(e_loclist)); 2667 return; 2668 } 2669 } 2670 2671 if (qi->qf_curlist >= qi->qf_listcount 2672 || qi->qf_lists[qi->qf_curlist].qf_count == 0) 2673 { 2674 EMSG(_(e_quickfix)); 2675 return; 2676 } 2677 if (*arg == '+') 2678 { 2679 ++arg; 2680 plus = TRUE; 2681 } 2682 if (!get_list_range(&arg, &idx1, &idx2) || *arg != NUL) 2683 { 2684 EMSG(_(e_trailing)); 2685 return; 2686 } 2687 if (plus) 2688 { 2689 i = qi->qf_lists[qi->qf_curlist].qf_index; 2690 idx2 = i + idx1; 2691 idx1 = i; 2692 } 2693 else 2694 { 2695 i = qi->qf_lists[qi->qf_curlist].qf_count; 2696 if (idx1 < 0) 2697 idx1 = (-idx1 > i) ? 0 : idx1 + i + 1; 2698 if (idx2 < 0) 2699 idx2 = (-idx2 > i) ? 0 : idx2 + i + 1; 2700 } 2701 2702 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid) 2703 all = TRUE; 2704 qfp = qi->qf_lists[qi->qf_curlist].qf_start; 2705 for (i = 1; !got_int && i <= qi->qf_lists[qi->qf_curlist].qf_count; ) 2706 { 2707 if ((qfp->qf_valid || all) && idx1 <= i && i <= idx2) 2708 { 2709 msg_putchar('\n'); 2710 if (got_int) 2711 break; 2712 2713 fname = NULL; 2714 if (qfp->qf_fnum != 0 2715 && (buf = buflist_findnr(qfp->qf_fnum)) != NULL) 2716 { 2717 fname = buf->b_fname; 2718 if (qfp->qf_type == 1) /* :helpgrep */ 2719 fname = gettail(fname); 2720 } 2721 if (fname == NULL) 2722 sprintf((char *)IObuff, "%2d", i); 2723 else 2724 vim_snprintf((char *)IObuff, IOSIZE, "%2d %s", 2725 i, (char *)fname); 2726 msg_outtrans_attr(IObuff, i == qi->qf_lists[qi->qf_curlist].qf_index 2727 ? HL_ATTR(HLF_QFL) : HL_ATTR(HLF_D)); 2728 if (qfp->qf_lnum == 0) 2729 IObuff[0] = NUL; 2730 else if (qfp->qf_col == 0) 2731 sprintf((char *)IObuff, ":%ld", qfp->qf_lnum); 2732 else 2733 sprintf((char *)IObuff, ":%ld col %d", 2734 qfp->qf_lnum, qfp->qf_col); 2735 sprintf((char *)IObuff + STRLEN(IObuff), "%s:", 2736 (char *)qf_types(qfp->qf_type, qfp->qf_nr)); 2737 msg_puts_attr(IObuff, HL_ATTR(HLF_N)); 2738 if (qfp->qf_pattern != NULL) 2739 { 2740 qf_fmt_text(qfp->qf_pattern, IObuff, IOSIZE); 2741 STRCAT(IObuff, ":"); 2742 msg_puts(IObuff); 2743 } 2744 msg_puts((char_u *)" "); 2745 2746 /* Remove newlines and leading whitespace from the text. For an 2747 * unrecognized line keep the indent, the compiler may mark a word 2748 * with ^^^^. */ 2749 qf_fmt_text((fname != NULL || qfp->qf_lnum != 0) 2750 ? skipwhite(qfp->qf_text) : qfp->qf_text, 2751 IObuff, IOSIZE); 2752 msg_prt_line(IObuff, FALSE); 2753 out_flush(); /* show one line at a time */ 2754 } 2755 2756 qfp = qfp->qf_next; 2757 if (qfp == NULL) 2758 break; 2759 ++i; 2760 ui_breakcheck(); 2761 } 2762 } 2763 2764 /* 2765 * Remove newlines and leading whitespace from an error message. 2766 * Put the result in "buf[bufsize]". 2767 */ 2768 static void 2769 qf_fmt_text(char_u *text, char_u *buf, int bufsize) 2770 { 2771 int i; 2772 char_u *p = text; 2773 2774 for (i = 0; *p != NUL && i < bufsize - 1; ++i) 2775 { 2776 if (*p == '\n') 2777 { 2778 buf[i] = ' '; 2779 while (*++p != NUL) 2780 if (!VIM_ISWHITE(*p) && *p != '\n') 2781 break; 2782 } 2783 else 2784 buf[i] = *p++; 2785 } 2786 buf[i] = NUL; 2787 } 2788 2789 static void 2790 qf_msg(qf_info_T *qi, int which, char *lead) 2791 { 2792 char *title = (char *)qi->qf_lists[which].qf_title; 2793 int count = qi->qf_lists[which].qf_count; 2794 char_u buf[IOSIZE]; 2795 2796 vim_snprintf((char *)buf, IOSIZE, _("%serror list %d of %d; %d errors "), 2797 lead, 2798 which + 1, 2799 qi->qf_listcount, 2800 count); 2801 2802 if (title != NULL) 2803 { 2804 size_t len = STRLEN(buf); 2805 2806 if (len < 34) 2807 { 2808 vim_memset(buf + len, ' ', 34 - len); 2809 buf[34] = NUL; 2810 } 2811 vim_strcat(buf, (char_u *)title, IOSIZE); 2812 } 2813 trunc_string(buf, buf, Columns - 1, IOSIZE); 2814 msg(buf); 2815 } 2816 2817 /* 2818 * ":colder [count]": Up in the quickfix stack. 2819 * ":cnewer [count]": Down in the quickfix stack. 2820 * ":lolder [count]": Up in the location list stack. 2821 * ":lnewer [count]": Down in the location list stack. 2822 */ 2823 void 2824 qf_age(exarg_T *eap) 2825 { 2826 qf_info_T *qi = &ql_info; 2827 int count; 2828 2829 if (eap->cmdidx == CMD_lolder || eap->cmdidx == CMD_lnewer) 2830 { 2831 qi = GET_LOC_LIST(curwin); 2832 if (qi == NULL) 2833 { 2834 EMSG(_(e_loclist)); 2835 return; 2836 } 2837 } 2838 2839 if (eap->addr_count != 0) 2840 count = eap->line2; 2841 else 2842 count = 1; 2843 while (count--) 2844 { 2845 if (eap->cmdidx == CMD_colder || eap->cmdidx == CMD_lolder) 2846 { 2847 if (qi->qf_curlist == 0) 2848 { 2849 EMSG(_("E380: At bottom of quickfix stack")); 2850 break; 2851 } 2852 --qi->qf_curlist; 2853 } 2854 else 2855 { 2856 if (qi->qf_curlist >= qi->qf_listcount - 1) 2857 { 2858 EMSG(_("E381: At top of quickfix stack")); 2859 break; 2860 } 2861 ++qi->qf_curlist; 2862 } 2863 } 2864 qf_msg(qi, qi->qf_curlist, ""); 2865 qf_update_buffer(qi, NULL); 2866 } 2867 2868 void 2869 qf_history(exarg_T *eap) 2870 { 2871 qf_info_T *qi = &ql_info; 2872 int i; 2873 2874 if (eap->cmdidx == CMD_lhistory) 2875 qi = GET_LOC_LIST(curwin); 2876 if (qi == NULL || (qi->qf_listcount == 0 2877 && qi->qf_lists[qi->qf_curlist].qf_count == 0)) 2878 MSG(_("No entries")); 2879 else 2880 for (i = 0; i < qi->qf_listcount; ++i) 2881 qf_msg(qi, i, i == qi->qf_curlist ? "> " : " "); 2882 } 2883 2884 /* 2885 * Free all the entries in the error list "idx". Note that other information 2886 * associated with the list like context and title are not freed. 2887 */ 2888 static void 2889 qf_free_items(qf_info_T *qi, int idx) 2890 { 2891 qfline_T *qfp; 2892 qfline_T *qfpnext; 2893 int stop = FALSE; 2894 qf_list_T *qfl = &qi->qf_lists[idx]; 2895 2896 while (qfl->qf_count && qfl->qf_start != NULL) 2897 { 2898 qfp = qfl->qf_start; 2899 qfpnext = qfp->qf_next; 2900 if (!stop) 2901 { 2902 vim_free(qfp->qf_text); 2903 stop = (qfp == qfpnext); 2904 vim_free(qfp->qf_pattern); 2905 vim_free(qfp); 2906 if (stop) 2907 /* Somehow qf_count may have an incorrect value, set it to 1 2908 * to avoid crashing when it's wrong. 2909 * TODO: Avoid qf_count being incorrect. */ 2910 qfl->qf_count = 1; 2911 } 2912 qfl->qf_start = qfpnext; 2913 --qfl->qf_count; 2914 } 2915 2916 qfl->qf_index = 0; 2917 qfl->qf_start = NULL; 2918 qfl->qf_last = NULL; 2919 qfl->qf_ptr = NULL; 2920 qfl->qf_nonevalid = TRUE; 2921 2922 qf_clean_dir_stack(&qfl->qf_dir_stack); 2923 qfl->qf_directory = NULL; 2924 qf_clean_dir_stack(&qfl->qf_file_stack); 2925 qfl->qf_currfile = NULL; 2926 qfl->qf_multiline = FALSE; 2927 qfl->qf_multiignore = FALSE; 2928 qfl->qf_multiscan = FALSE; 2929 } 2930 2931 /* 2932 * Free error list "idx". Frees all the entries in the quickfix list, 2933 * associated context information and the title. 2934 */ 2935 static void 2936 qf_free(qf_info_T *qi, int idx) 2937 { 2938 qf_list_T *qfl = &qi->qf_lists[idx]; 2939 2940 qf_free_items(qi, idx); 2941 2942 vim_free(qfl->qf_title); 2943 qfl->qf_title = NULL; 2944 free_tv(qfl->qf_ctx); 2945 qfl->qf_ctx = NULL; 2946 qfl->qf_id = 0; 2947 } 2948 2949 /* 2950 * qf_mark_adjust: adjust marks 2951 */ 2952 void 2953 qf_mark_adjust( 2954 win_T *wp, 2955 linenr_T line1, 2956 linenr_T line2, 2957 long amount, 2958 long amount_after) 2959 { 2960 int i; 2961 qfline_T *qfp; 2962 int idx; 2963 qf_info_T *qi = &ql_info; 2964 int found_one = FALSE; 2965 int buf_has_flag = wp == NULL ? BUF_HAS_QF_ENTRY : BUF_HAS_LL_ENTRY; 2966 2967 if (!(curbuf->b_has_qf_entry & buf_has_flag)) 2968 return; 2969 if (wp != NULL) 2970 { 2971 if (wp->w_llist == NULL) 2972 return; 2973 qi = wp->w_llist; 2974 } 2975 2976 for (idx = 0; idx < qi->qf_listcount; ++idx) 2977 if (qi->qf_lists[idx].qf_count) 2978 for (i = 0, qfp = qi->qf_lists[idx].qf_start; 2979 i < qi->qf_lists[idx].qf_count && qfp != NULL; 2980 ++i, qfp = qfp->qf_next) 2981 if (qfp->qf_fnum == curbuf->b_fnum) 2982 { 2983 found_one = TRUE; 2984 if (qfp->qf_lnum >= line1 && qfp->qf_lnum <= line2) 2985 { 2986 if (amount == MAXLNUM) 2987 qfp->qf_cleared = TRUE; 2988 else 2989 qfp->qf_lnum += amount; 2990 } 2991 else if (amount_after && qfp->qf_lnum > line2) 2992 qfp->qf_lnum += amount_after; 2993 } 2994 2995 if (!found_one) 2996 curbuf->b_has_qf_entry &= ~buf_has_flag; 2997 } 2998 2999 /* 3000 * Make a nice message out of the error character and the error number: 3001 * char number message 3002 * e or E 0 " error" 3003 * w or W 0 " warning" 3004 * i or I 0 " info" 3005 * 0 0 "" 3006 * other 0 " c" 3007 * e or E n " error n" 3008 * w or W n " warning n" 3009 * i or I n " info n" 3010 * 0 n " error n" 3011 * other n " c n" 3012 * 1 x "" :helpgrep 3013 */ 3014 static char_u * 3015 qf_types(int c, int nr) 3016 { 3017 static char_u buf[20]; 3018 static char_u cc[3]; 3019 char_u *p; 3020 3021 if (c == 'W' || c == 'w') 3022 p = (char_u *)" warning"; 3023 else if (c == 'I' || c == 'i') 3024 p = (char_u *)" info"; 3025 else if (c == 'E' || c == 'e' || (c == 0 && nr > 0)) 3026 p = (char_u *)" error"; 3027 else if (c == 0 || c == 1) 3028 p = (char_u *)""; 3029 else 3030 { 3031 cc[0] = ' '; 3032 cc[1] = c; 3033 cc[2] = NUL; 3034 p = cc; 3035 } 3036 3037 if (nr <= 0) 3038 return p; 3039 3040 sprintf((char *)buf, "%s %3d", (char *)p, nr); 3041 return buf; 3042 } 3043 3044 /* 3045 * ":cwindow": open the quickfix window if we have errors to display, 3046 * close it if not. 3047 * ":lwindow": open the location list window if we have locations to display, 3048 * close it if not. 3049 */ 3050 void 3051 ex_cwindow(exarg_T *eap) 3052 { 3053 qf_info_T *qi = &ql_info; 3054 win_T *win; 3055 3056 if (eap->cmdidx == CMD_lwindow) 3057 { 3058 qi = GET_LOC_LIST(curwin); 3059 if (qi == NULL) 3060 return; 3061 } 3062 3063 /* Look for an existing quickfix window. */ 3064 win = qf_find_win(qi); 3065 3066 /* 3067 * If a quickfix window is open but we have no errors to display, 3068 * close the window. If a quickfix window is not open, then open 3069 * it if we have errors; otherwise, leave it closed. 3070 */ 3071 if (qi->qf_lists[qi->qf_curlist].qf_nonevalid 3072 || qi->qf_lists[qi->qf_curlist].qf_count == 0 3073 || qi->qf_curlist >= qi->qf_listcount) 3074 { 3075 if (win != NULL) 3076 ex_cclose(eap); 3077 } 3078 else if (win == NULL) 3079 ex_copen(eap); 3080 } 3081 3082 /* 3083 * ":cclose": close the window showing the list of errors. 3084 * ":lclose": close the window showing the location list 3085 */ 3086 void 3087 ex_cclose(exarg_T *eap) 3088 { 3089 win_T *win = NULL; 3090 qf_info_T *qi = &ql_info; 3091 3092 if (eap->cmdidx == CMD_lclose || eap->cmdidx == CMD_lwindow) 3093 { 3094 qi = GET_LOC_LIST(curwin); 3095 if (qi == NULL) 3096 return; 3097 } 3098 3099 /* Find existing quickfix window and close it. */ 3100 win = qf_find_win(qi); 3101 if (win != NULL) 3102 win_close(win, FALSE); 3103 } 3104 3105 /* 3106 * ":copen": open a window that shows the list of errors. 3107 * ":lopen": open a window that shows the location list. 3108 */ 3109 void 3110 ex_copen(exarg_T *eap) 3111 { 3112 qf_info_T *qi = &ql_info; 3113 int height; 3114 win_T *win; 3115 tabpage_T *prevtab = curtab; 3116 buf_T *qf_buf; 3117 win_T *oldwin = curwin; 3118 3119 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) 3120 { 3121 qi = GET_LOC_LIST(curwin); 3122 if (qi == NULL) 3123 { 3124 EMSG(_(e_loclist)); 3125 return; 3126 } 3127 } 3128 3129 if (eap->addr_count != 0) 3130 height = eap->line2; 3131 else 3132 height = QF_WINHEIGHT; 3133 3134 reset_VIsual_and_resel(); /* stop Visual mode */ 3135 #ifdef FEAT_GUI 3136 need_mouse_correct = TRUE; 3137 #endif 3138 3139 /* 3140 * Find existing quickfix window, or open a new one. 3141 */ 3142 win = qf_find_win(qi); 3143 3144 if (win != NULL && cmdmod.tab == 0) 3145 { 3146 win_goto(win); 3147 if (eap->addr_count != 0) 3148 { 3149 if (cmdmod.split & WSP_VERT) 3150 { 3151 if (height != win->w_width) 3152 win_setwidth(height); 3153 } 3154 else if (height != win->w_height) 3155 win_setheight(height); 3156 } 3157 } 3158 else 3159 { 3160 qf_buf = qf_find_buf(qi); 3161 3162 /* The current window becomes the previous window afterwards. */ 3163 win = curwin; 3164 3165 if ((eap->cmdidx == CMD_copen || eap->cmdidx == CMD_cwindow) 3166 && cmdmod.split == 0) 3167 /* Create the new window at the very bottom, except when 3168 * :belowright or :aboveleft is used. */ 3169 win_goto(lastwin); 3170 if (win_split(height, WSP_BELOW | WSP_NEWLOC) == FAIL) 3171 return; /* not enough room for window */ 3172 RESET_BINDING(curwin); 3173 3174 if (eap->cmdidx == CMD_lopen || eap->cmdidx == CMD_lwindow) 3175 { 3176 /* 3177 * For the location list window, create a reference to the 3178 * location list from the window 'win'. 3179 */ 3180 curwin->w_llist_ref = win->w_llist; 3181 win->w_llist->qf_refcount++; 3182 } 3183 3184 if (oldwin != curwin) 3185 oldwin = NULL; /* don't store info when in another window */ 3186 if (qf_buf != NULL) 3187 /* Use the existing quickfix buffer */ 3188 (void)do_ecmd(qf_buf->b_fnum, NULL, NULL, NULL, ECMD_ONE, 3189 ECMD_HIDE + ECMD_OLDBUF, oldwin); 3190 else 3191 { 3192 /* Create a new quickfix buffer */ 3193 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, oldwin); 3194 /* switch off 'swapfile' */ 3195 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); 3196 set_option_value((char_u *)"bt", 0L, (char_u *)"quickfix", 3197 OPT_LOCAL); 3198 set_option_value((char_u *)"bh", 0L, (char_u *)"wipe", OPT_LOCAL); 3199 RESET_BINDING(curwin); 3200 #ifdef FEAT_DIFF 3201 curwin->w_p_diff = FALSE; 3202 #endif 3203 #ifdef FEAT_FOLDING 3204 set_option_value((char_u *)"fdm", 0L, (char_u *)"manual", 3205 OPT_LOCAL); 3206 #endif 3207 } 3208 3209 /* Only set the height when still in the same tab page and there is no 3210 * window to the side. */ 3211 if (curtab == prevtab && curwin->w_width == Columns) 3212 win_setheight(height); 3213 curwin->w_p_wfh = TRUE; /* set 'winfixheight' */ 3214 if (win_valid(win)) 3215 prevwin = win; 3216 } 3217 3218 qf_set_title_var(qi); 3219 3220 /* 3221 * Fill the buffer with the quickfix list. 3222 */ 3223 qf_fill_buffer(qi, curbuf, NULL); 3224 3225 curwin->w_cursor.lnum = qi->qf_lists[qi->qf_curlist].qf_index; 3226 curwin->w_cursor.col = 0; 3227 check_cursor(); 3228 update_topline(); /* scroll to show the line */ 3229 } 3230 3231 /* 3232 * Move the cursor in the quickfix window to "lnum". 3233 */ 3234 static void 3235 qf_win_goto(win_T *win, linenr_T lnum) 3236 { 3237 win_T *old_curwin = curwin; 3238 3239 curwin = win; 3240 curbuf = win->w_buffer; 3241 curwin->w_cursor.lnum = lnum; 3242 curwin->w_cursor.col = 0; 3243 #ifdef FEAT_VIRTUALEDIT 3244 curwin->w_cursor.coladd = 0; 3245 #endif 3246 curwin->w_curswant = 0; 3247 update_topline(); /* scroll to show the line */ 3248 redraw_later(VALID); 3249 curwin->w_redr_status = TRUE; /* update ruler */ 3250 curwin = old_curwin; 3251 curbuf = curwin->w_buffer; 3252 } 3253 3254 /* 3255 * :cbottom/:lbottom commands. 3256 */ 3257 void 3258 ex_cbottom(exarg_T *eap UNUSED) 3259 { 3260 qf_info_T *qi = &ql_info; 3261 win_T *win; 3262 3263 if (eap->cmdidx == CMD_lbottom) 3264 { 3265 qi = GET_LOC_LIST(curwin); 3266 if (qi == NULL) 3267 { 3268 EMSG(_(e_loclist)); 3269 return; 3270 } 3271 } 3272 3273 win = qf_find_win(qi); 3274 if (win != NULL && win->w_cursor.lnum != win->w_buffer->b_ml.ml_line_count) 3275 qf_win_goto(win, win->w_buffer->b_ml.ml_line_count); 3276 } 3277 3278 /* 3279 * Return the number of the current entry (line number in the quickfix 3280 * window). 3281 */ 3282 linenr_T 3283 qf_current_entry(win_T *wp) 3284 { 3285 qf_info_T *qi = &ql_info; 3286 3287 if (IS_LL_WINDOW(wp)) 3288 /* In the location list window, use the referenced location list */ 3289 qi = wp->w_llist_ref; 3290 3291 return qi->qf_lists[qi->qf_curlist].qf_index; 3292 } 3293 3294 /* 3295 * Update the cursor position in the quickfix window to the current error. 3296 * Return TRUE if there is a quickfix window. 3297 */ 3298 static int 3299 qf_win_pos_update( 3300 qf_info_T *qi, 3301 int old_qf_index) /* previous qf_index or zero */ 3302 { 3303 win_T *win; 3304 int qf_index = qi->qf_lists[qi->qf_curlist].qf_index; 3305 3306 /* 3307 * Put the cursor on the current error in the quickfix window, so that 3308 * it's viewable. 3309 */ 3310 win = qf_find_win(qi); 3311 if (win != NULL 3312 && qf_index <= win->w_buffer->b_ml.ml_line_count 3313 && old_qf_index != qf_index) 3314 { 3315 if (qf_index > old_qf_index) 3316 { 3317 win->w_redraw_top = old_qf_index; 3318 win->w_redraw_bot = qf_index; 3319 } 3320 else 3321 { 3322 win->w_redraw_top = qf_index; 3323 win->w_redraw_bot = old_qf_index; 3324 } 3325 qf_win_goto(win, qf_index); 3326 } 3327 return win != NULL; 3328 } 3329 3330 /* 3331 * Check whether the given window is displaying the specified quickfix/location 3332 * list buffer 3333 */ 3334 static int 3335 is_qf_win(win_T *win, qf_info_T *qi) 3336 { 3337 /* 3338 * A window displaying the quickfix buffer will have the w_llist_ref field 3339 * set to NULL. 3340 * A window displaying a location list buffer will have the w_llist_ref 3341 * pointing to the location list. 3342 */ 3343 if (bt_quickfix(win->w_buffer)) 3344 if ((qi == &ql_info && win->w_llist_ref == NULL) 3345 || (qi != &ql_info && win->w_llist_ref == qi)) 3346 return TRUE; 3347 3348 return FALSE; 3349 } 3350 3351 /* 3352 * Find a window displaying the quickfix/location list 'qi' 3353 * Searches in only the windows opened in the current tab. 3354 */ 3355 static win_T * 3356 qf_find_win(qf_info_T *qi) 3357 { 3358 win_T *win; 3359 3360 FOR_ALL_WINDOWS(win) 3361 if (is_qf_win(win, qi)) 3362 break; 3363 3364 return win; 3365 } 3366 3367 /* 3368 * Find a quickfix buffer. 3369 * Searches in windows opened in all the tabs. 3370 */ 3371 static buf_T * 3372 qf_find_buf(qf_info_T *qi) 3373 { 3374 tabpage_T *tp; 3375 win_T *win; 3376 3377 FOR_ALL_TAB_WINDOWS(tp, win) 3378 if (is_qf_win(win, qi)) 3379 return win->w_buffer; 3380 3381 return NULL; 3382 } 3383 3384 /* 3385 * Update the w:quickfix_title variable in the quickfix/location list window 3386 */ 3387 static void 3388 qf_update_win_titlevar(qf_info_T *qi) 3389 { 3390 win_T *win; 3391 win_T *curwin_save; 3392 3393 if ((win = qf_find_win(qi)) != NULL) 3394 { 3395 curwin_save = curwin; 3396 curwin = win; 3397 qf_set_title_var(qi); 3398 curwin = curwin_save; 3399 } 3400 } 3401 3402 /* 3403 * Find the quickfix buffer. If it exists, update the contents. 3404 */ 3405 static void 3406 qf_update_buffer(qf_info_T *qi, qfline_T *old_last) 3407 { 3408 buf_T *buf; 3409 win_T *win; 3410 aco_save_T aco; 3411 3412 /* Check if a buffer for the quickfix list exists. Update it. */ 3413 buf = qf_find_buf(qi); 3414 if (buf != NULL) 3415 { 3416 linenr_T old_line_count = buf->b_ml.ml_line_count; 3417 3418 if (old_last == NULL) 3419 /* set curwin/curbuf to buf and save a few things */ 3420 aucmd_prepbuf(&aco, buf); 3421 3422 qf_update_win_titlevar(qi); 3423 3424 qf_fill_buffer(qi, buf, old_last); 3425 ++CHANGEDTICK(buf); 3426 3427 if (old_last == NULL) 3428 { 3429 (void)qf_win_pos_update(qi, 0); 3430 3431 /* restore curwin/curbuf and a few other things */ 3432 aucmd_restbuf(&aco); 3433 } 3434 3435 /* Only redraw when added lines are visible. This avoids flickering 3436 * when the added lines are not visible. */ 3437 if ((win = qf_find_win(qi)) != NULL && old_line_count < win->w_botline) 3438 redraw_buf_later(buf, NOT_VALID); 3439 } 3440 } 3441 3442 /* 3443 * Set "w:quickfix_title" if "qi" has a title. 3444 */ 3445 static void 3446 qf_set_title_var(qf_info_T *qi) 3447 { 3448 if (qi->qf_lists[qi->qf_curlist].qf_title != NULL) 3449 set_internal_string_var((char_u *)"w:quickfix_title", 3450 qi->qf_lists[qi->qf_curlist].qf_title); 3451 } 3452 3453 /* 3454 * Fill current buffer with quickfix errors, replacing any previous contents. 3455 * curbuf must be the quickfix buffer! 3456 * If "old_last" is not NULL append the items after this one. 3457 * When "old_last" is NULL then "buf" must equal "curbuf"! Because 3458 * ml_delete() is used and autocommands will be triggered. 3459 */ 3460 static void 3461 qf_fill_buffer(qf_info_T *qi, buf_T *buf, qfline_T *old_last) 3462 { 3463 linenr_T lnum; 3464 qfline_T *qfp; 3465 buf_T *errbuf; 3466 int len; 3467 int old_KeyTyped = KeyTyped; 3468 3469 if (old_last == NULL) 3470 { 3471 if (buf != curbuf) 3472 { 3473 internal_error("qf_fill_buffer()"); 3474 return; 3475 } 3476 3477 /* delete all existing lines */ 3478 while ((curbuf->b_ml.ml_flags & ML_EMPTY) == 0) 3479 (void)ml_delete((linenr_T)1, FALSE); 3480 } 3481 3482 /* Check if there is anything to display */ 3483 if (qi->qf_curlist < qi->qf_listcount) 3484 { 3485 /* Add one line for each error */ 3486 if (old_last == NULL) 3487 { 3488 qfp = qi->qf_lists[qi->qf_curlist].qf_start; 3489 lnum = 0; 3490 } 3491 else 3492 { 3493 qfp = old_last->qf_next; 3494 lnum = buf->b_ml.ml_line_count; 3495 } 3496 while (lnum < qi->qf_lists[qi->qf_curlist].qf_count) 3497 { 3498 if (qfp->qf_fnum != 0 3499 && (errbuf = buflist_findnr(qfp->qf_fnum)) != NULL 3500 && errbuf->b_fname != NULL) 3501 { 3502 if (qfp->qf_type == 1) /* :helpgrep */ 3503 STRCPY(IObuff, gettail(errbuf->b_fname)); 3504 else 3505 STRCPY(IObuff, errbuf->b_fname); 3506 len = (int)STRLEN(IObuff); 3507 } 3508 else 3509 len = 0; 3510 IObuff[len++] = '|'; 3511 3512 if (qfp->qf_lnum > 0) 3513 { 3514 sprintf((char *)IObuff + len, "%ld", qfp->qf_lnum); 3515 len += (int)STRLEN(IObuff + len); 3516 3517 if (qfp->qf_col > 0) 3518 { 3519 sprintf((char *)IObuff + len, " col %d", qfp->qf_col); 3520 len += (int)STRLEN(IObuff + len); 3521 } 3522 3523 sprintf((char *)IObuff + len, "%s", 3524 (char *)qf_types(qfp->qf_type, qfp->qf_nr)); 3525 len += (int)STRLEN(IObuff + len); 3526 } 3527 else if (qfp->qf_pattern != NULL) 3528 { 3529 qf_fmt_text(qfp->qf_pattern, IObuff + len, IOSIZE - len); 3530 len += (int)STRLEN(IObuff + len); 3531 } 3532 IObuff[len++] = '|'; 3533 IObuff[len++] = ' '; 3534 3535 /* Remove newlines and leading whitespace from the text. 3536 * For an unrecognized line keep the indent, the compiler may 3537 * mark a word with ^^^^. */ 3538 qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text, 3539 IObuff + len, IOSIZE - len); 3540 3541 if (ml_append_buf(buf, lnum, IObuff, 3542 (colnr_T)STRLEN(IObuff) + 1, FALSE) == FAIL) 3543 break; 3544 ++lnum; 3545 qfp = qfp->qf_next; 3546 if (qfp == NULL) 3547 break; 3548 } 3549 3550 if (old_last == NULL) 3551 /* Delete the empty line which is now at the end */ 3552 (void)ml_delete(lnum + 1, FALSE); 3553 } 3554 3555 /* correct cursor position */ 3556 check_lnums(TRUE); 3557 3558 if (old_last == NULL) 3559 { 3560 /* Set the 'filetype' to "qf" each time after filling the buffer. 3561 * This resembles reading a file into a buffer, it's more logical when 3562 * using autocommands. */ 3563 #ifdef FEAT_AUTOCMD 3564 ++curbuf_lock; 3565 #endif 3566 set_option_value((char_u *)"ft", 0L, (char_u *)"qf", OPT_LOCAL); 3567 curbuf->b_p_ma = FALSE; 3568 3569 #ifdef FEAT_AUTOCMD 3570 keep_filetype = TRUE; /* don't detect 'filetype' */ 3571 apply_autocmds(EVENT_BUFREADPOST, (char_u *)"quickfix", NULL, 3572 FALSE, curbuf); 3573 apply_autocmds(EVENT_BUFWINENTER, (char_u *)"quickfix", NULL, 3574 FALSE, curbuf); 3575 keep_filetype = FALSE; 3576 --curbuf_lock; 3577 #endif 3578 /* make sure it will be redrawn */ 3579 redraw_curbuf_later(NOT_VALID); 3580 } 3581 3582 /* Restore KeyTyped, setting 'filetype' may reset it. */ 3583 KeyTyped = old_KeyTyped; 3584 } 3585 3586 /* 3587 * Return TRUE when using ":vimgrep" for ":grep". 3588 */ 3589 int 3590 grep_internal(cmdidx_T cmdidx) 3591 { 3592 return ((cmdidx == CMD_grep 3593 || cmdidx == CMD_lgrep 3594 || cmdidx == CMD_grepadd 3595 || cmdidx == CMD_lgrepadd) 3596 && STRCMP("internal", 3597 *curbuf->b_p_gp == NUL ? p_gp : curbuf->b_p_gp) == 0); 3598 } 3599 3600 /* 3601 * Used for ":make", ":lmake", ":grep", ":lgrep", ":grepadd", and ":lgrepadd" 3602 */ 3603 void 3604 ex_make(exarg_T *eap) 3605 { 3606 char_u *fname; 3607 char_u *cmd; 3608 char_u *enc = NULL; 3609 unsigned len; 3610 win_T *wp = NULL; 3611 qf_info_T *qi = &ql_info; 3612 int res; 3613 #ifdef FEAT_AUTOCMD 3614 char_u *au_name = NULL; 3615 3616 /* Redirect ":grep" to ":vimgrep" if 'grepprg' is "internal". */ 3617 if (grep_internal(eap->cmdidx)) 3618 { 3619 ex_vimgrep(eap); 3620 return; 3621 } 3622 3623 switch (eap->cmdidx) 3624 { 3625 case CMD_make: au_name = (char_u *)"make"; break; 3626 case CMD_lmake: au_name = (char_u *)"lmake"; break; 3627 case CMD_grep: au_name = (char_u *)"grep"; break; 3628 case CMD_lgrep: au_name = (char_u *)"lgrep"; break; 3629 case CMD_grepadd: au_name = (char_u *)"grepadd"; break; 3630 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break; 3631 default: break; 3632 } 3633 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 3634 curbuf->b_fname, TRUE, curbuf)) 3635 { 3636 # ifdef FEAT_EVAL 3637 if (aborting()) 3638 return; 3639 # endif 3640 } 3641 #endif 3642 #ifdef FEAT_MBYTE 3643 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc; 3644 #endif 3645 3646 if (eap->cmdidx == CMD_lmake || eap->cmdidx == CMD_lgrep 3647 || eap->cmdidx == CMD_lgrepadd) 3648 wp = curwin; 3649 3650 autowrite_all(); 3651 fname = get_mef_name(); 3652 if (fname == NULL) 3653 return; 3654 mch_remove(fname); /* in case it's not unique */ 3655 3656 /* 3657 * If 'shellpipe' empty: don't redirect to 'errorfile'. 3658 */ 3659 len = (unsigned)STRLEN(p_shq) * 2 + (unsigned)STRLEN(eap->arg) + 1; 3660 if (*p_sp != NUL) 3661 len += (unsigned)STRLEN(p_sp) + (unsigned)STRLEN(fname) + 3; 3662 cmd = alloc(len); 3663 if (cmd == NULL) 3664 return; 3665 sprintf((char *)cmd, "%s%s%s", (char *)p_shq, (char *)eap->arg, 3666 (char *)p_shq); 3667 if (*p_sp != NUL) 3668 append_redir(cmd, len, p_sp, fname); 3669 /* 3670 * Output a newline if there's something else than the :make command that 3671 * was typed (in which case the cursor is in column 0). 3672 */ 3673 if (msg_col == 0) 3674 msg_didout = FALSE; 3675 msg_start(); 3676 MSG_PUTS(":!"); 3677 msg_outtrans(cmd); /* show what we are doing */ 3678 3679 /* let the shell know if we are redirecting output or not */ 3680 do_shell(cmd, *p_sp != NUL ? SHELL_DOOUT : 0); 3681 3682 #ifdef AMIGA 3683 out_flush(); 3684 /* read window status report and redraw before message */ 3685 (void)char_avail(); 3686 #endif 3687 3688 res = qf_init(wp, fname, (eap->cmdidx != CMD_make 3689 && eap->cmdidx != CMD_lmake) ? p_gefm : p_efm, 3690 (eap->cmdidx != CMD_grepadd 3691 && eap->cmdidx != CMD_lgrepadd), 3692 *eap->cmdlinep, enc); 3693 if (wp != NULL) 3694 qi = GET_LOC_LIST(wp); 3695 #ifdef FEAT_AUTOCMD 3696 if (au_name != NULL) 3697 { 3698 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 3699 curbuf->b_fname, TRUE, curbuf); 3700 if (qi->qf_curlist < qi->qf_listcount) 3701 res = qi->qf_lists[qi->qf_curlist].qf_count; 3702 else 3703 res = 0; 3704 } 3705 #endif 3706 if (res > 0 && !eap->forceit) 3707 qf_jump(qi, 0, 0, FALSE); /* display first error */ 3708 3709 mch_remove(fname); 3710 vim_free(fname); 3711 vim_free(cmd); 3712 } 3713 3714 /* 3715 * Return the name for the errorfile, in allocated memory. 3716 * Find a new unique name when 'makeef' contains "##". 3717 * Returns NULL for error. 3718 */ 3719 static char_u * 3720 get_mef_name(void) 3721 { 3722 char_u *p; 3723 char_u *name; 3724 static int start = -1; 3725 static int off = 0; 3726 #ifdef HAVE_LSTAT 3727 stat_T sb; 3728 #endif 3729 3730 if (*p_mef == NUL) 3731 { 3732 name = vim_tempname('e', FALSE); 3733 if (name == NULL) 3734 EMSG(_(e_notmp)); 3735 return name; 3736 } 3737 3738 for (p = p_mef; *p; ++p) 3739 if (p[0] == '#' && p[1] == '#') 3740 break; 3741 3742 if (*p == NUL) 3743 return vim_strsave(p_mef); 3744 3745 /* Keep trying until the name doesn't exist yet. */ 3746 for (;;) 3747 { 3748 if (start == -1) 3749 start = mch_get_pid(); 3750 else 3751 off += 19; 3752 3753 name = alloc((unsigned)STRLEN(p_mef) + 30); 3754 if (name == NULL) 3755 break; 3756 STRCPY(name, p_mef); 3757 sprintf((char *)name + (p - p_mef), "%d%d", start, off); 3758 STRCAT(name, p + 2); 3759 if (mch_getperm(name) < 0 3760 #ifdef HAVE_LSTAT 3761 /* Don't accept a symbolic link, it's a security risk. */ 3762 && mch_lstat((char *)name, &sb) < 0 3763 #endif 3764 ) 3765 break; 3766 vim_free(name); 3767 } 3768 return name; 3769 } 3770 3771 /* 3772 * Returns the number of valid entries in the current quickfix/location list. 3773 */ 3774 int 3775 qf_get_size(exarg_T *eap) 3776 { 3777 qf_info_T *qi = &ql_info; 3778 qfline_T *qfp; 3779 int i, sz = 0; 3780 int prev_fnum = 0; 3781 3782 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3783 { 3784 /* Location list */ 3785 qi = GET_LOC_LIST(curwin); 3786 if (qi == NULL) 3787 return 0; 3788 } 3789 3790 for (i = 0, qfp = qi->qf_lists[qi->qf_curlist].qf_start; 3791 i < qi->qf_lists[qi->qf_curlist].qf_count && qfp != NULL; 3792 ++i, qfp = qfp->qf_next) 3793 { 3794 if (qfp->qf_valid) 3795 { 3796 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo) 3797 sz++; /* Count all valid entries */ 3798 else if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3799 { 3800 /* Count the number of files */ 3801 sz++; 3802 prev_fnum = qfp->qf_fnum; 3803 } 3804 } 3805 } 3806 3807 return sz; 3808 } 3809 3810 /* 3811 * Returns the current index of the quickfix/location list. 3812 * Returns 0 if there is an error. 3813 */ 3814 int 3815 qf_get_cur_idx(exarg_T *eap) 3816 { 3817 qf_info_T *qi = &ql_info; 3818 3819 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3820 { 3821 /* Location list */ 3822 qi = GET_LOC_LIST(curwin); 3823 if (qi == NULL) 3824 return 0; 3825 } 3826 3827 return qi->qf_lists[qi->qf_curlist].qf_index; 3828 } 3829 3830 /* 3831 * Returns the current index in the quickfix/location list (counting only valid 3832 * entries). If no valid entries are in the list, then returns 1. 3833 */ 3834 int 3835 qf_get_cur_valid_idx(exarg_T *eap) 3836 { 3837 qf_info_T *qi = &ql_info; 3838 qf_list_T *qfl; 3839 qfline_T *qfp; 3840 int i, eidx = 0; 3841 int prev_fnum = 0; 3842 3843 if (eap->cmdidx == CMD_ldo || eap->cmdidx == CMD_lfdo) 3844 { 3845 /* Location list */ 3846 qi = GET_LOC_LIST(curwin); 3847 if (qi == NULL) 3848 return 1; 3849 } 3850 3851 qfl = &qi->qf_lists[qi->qf_curlist]; 3852 qfp = qfl->qf_start; 3853 3854 /* check if the list has valid errors */ 3855 if (qfl->qf_count <= 0 || qfl->qf_nonevalid) 3856 return 1; 3857 3858 for (i = 1; i <= qfl->qf_index && qfp!= NULL; i++, qfp = qfp->qf_next) 3859 { 3860 if (qfp->qf_valid) 3861 { 3862 if (eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 3863 { 3864 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3865 { 3866 /* Count the number of files */ 3867 eidx++; 3868 prev_fnum = qfp->qf_fnum; 3869 } 3870 } 3871 else 3872 eidx++; 3873 } 3874 } 3875 3876 return eidx ? eidx : 1; 3877 } 3878 3879 /* 3880 * Get the 'n'th valid error entry in the quickfix or location list. 3881 * Used by :cdo, :ldo, :cfdo and :lfdo commands. 3882 * For :cdo and :ldo returns the 'n'th valid error entry. 3883 * For :cfdo and :lfdo returns the 'n'th valid file entry. 3884 */ 3885 static int 3886 qf_get_nth_valid_entry(qf_info_T *qi, int n, int fdo) 3887 { 3888 qf_list_T *qfl = &qi->qf_lists[qi->qf_curlist]; 3889 qfline_T *qfp = qfl->qf_start; 3890 int i, eidx; 3891 int prev_fnum = 0; 3892 3893 /* check if the list has valid errors */ 3894 if (qfl->qf_count <= 0 || qfl->qf_nonevalid) 3895 return 1; 3896 3897 for (i = 1, eidx = 0; i <= qfl->qf_count && qfp != NULL; 3898 i++, qfp = qfp->qf_next) 3899 { 3900 if (qfp->qf_valid) 3901 { 3902 if (fdo) 3903 { 3904 if (qfp->qf_fnum > 0 && qfp->qf_fnum != prev_fnum) 3905 { 3906 /* Count the number of files */ 3907 eidx++; 3908 prev_fnum = qfp->qf_fnum; 3909 } 3910 } 3911 else 3912 eidx++; 3913 } 3914 3915 if (eidx == n) 3916 break; 3917 } 3918 3919 if (i <= qfl->qf_count) 3920 return i; 3921 else 3922 return 1; 3923 } 3924 3925 /* 3926 * ":cc", ":crewind", ":cfirst" and ":clast". 3927 * ":ll", ":lrewind", ":lfirst" and ":llast". 3928 * ":cdo", ":ldo", ":cfdo" and ":lfdo" 3929 */ 3930 void 3931 ex_cc(exarg_T *eap) 3932 { 3933 qf_info_T *qi = &ql_info; 3934 int errornr; 3935 3936 if (eap->cmdidx == CMD_ll 3937 || eap->cmdidx == CMD_lrewind 3938 || eap->cmdidx == CMD_lfirst 3939 || eap->cmdidx == CMD_llast 3940 || eap->cmdidx == CMD_ldo 3941 || eap->cmdidx == CMD_lfdo) 3942 { 3943 qi = GET_LOC_LIST(curwin); 3944 if (qi == NULL) 3945 { 3946 EMSG(_(e_loclist)); 3947 return; 3948 } 3949 } 3950 3951 if (eap->addr_count > 0) 3952 errornr = (int)eap->line2; 3953 else 3954 { 3955 if (eap->cmdidx == CMD_cc || eap->cmdidx == CMD_ll) 3956 errornr = 0; 3957 else if (eap->cmdidx == CMD_crewind || eap->cmdidx == CMD_lrewind 3958 || eap->cmdidx == CMD_cfirst || eap->cmdidx == CMD_lfirst) 3959 errornr = 1; 3960 else 3961 errornr = 32767; 3962 } 3963 3964 /* For cdo and ldo commands, jump to the nth valid error. 3965 * For cfdo and lfdo commands, jump to the nth valid file entry. 3966 */ 3967 if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo 3968 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 3969 errornr = qf_get_nth_valid_entry(qi, 3970 eap->addr_count > 0 ? (int)eap->line1 : 1, 3971 eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo); 3972 3973 qf_jump(qi, 0, errornr, eap->forceit); 3974 } 3975 3976 /* 3977 * ":cnext", ":cnfile", ":cNext" and ":cprevious". 3978 * ":lnext", ":lNext", ":lprevious", ":lnfile", ":lNfile" and ":lpfile". 3979 * Also, used by ":cdo", ":ldo", ":cfdo" and ":lfdo" commands. 3980 */ 3981 void 3982 ex_cnext(exarg_T *eap) 3983 { 3984 qf_info_T *qi = &ql_info; 3985 int errornr; 3986 3987 if (eap->cmdidx == CMD_lnext 3988 || eap->cmdidx == CMD_lNext 3989 || eap->cmdidx == CMD_lprevious 3990 || eap->cmdidx == CMD_lnfile 3991 || eap->cmdidx == CMD_lNfile 3992 || eap->cmdidx == CMD_lpfile 3993 || eap->cmdidx == CMD_ldo 3994 || eap->cmdidx == CMD_lfdo) 3995 { 3996 qi = GET_LOC_LIST(curwin); 3997 if (qi == NULL) 3998 { 3999 EMSG(_(e_loclist)); 4000 return; 4001 } 4002 } 4003 4004 if (eap->addr_count > 0 4005 && (eap->cmdidx != CMD_cdo && eap->cmdidx != CMD_ldo 4006 && eap->cmdidx != CMD_cfdo && eap->cmdidx != CMD_lfdo)) 4007 errornr = (int)eap->line2; 4008 else 4009 errornr = 1; 4010 4011 qf_jump(qi, (eap->cmdidx == CMD_cnext || eap->cmdidx == CMD_lnext 4012 || eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo) 4013 ? FORWARD 4014 : (eap->cmdidx == CMD_cnfile || eap->cmdidx == CMD_lnfile 4015 || eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo) 4016 ? FORWARD_FILE 4017 : (eap->cmdidx == CMD_cpfile || eap->cmdidx == CMD_lpfile 4018 || eap->cmdidx == CMD_cNfile || eap->cmdidx == CMD_lNfile) 4019 ? BACKWARD_FILE 4020 : BACKWARD, 4021 errornr, eap->forceit); 4022 } 4023 4024 /* 4025 * ":cfile"/":cgetfile"/":caddfile" commands. 4026 * ":lfile"/":lgetfile"/":laddfile" commands. 4027 */ 4028 void 4029 ex_cfile(exarg_T *eap) 4030 { 4031 char_u *enc = NULL; 4032 win_T *wp = NULL; 4033 qf_info_T *qi = &ql_info; 4034 #ifdef FEAT_AUTOCMD 4035 char_u *au_name = NULL; 4036 #endif 4037 4038 if (eap->cmdidx == CMD_lfile || eap->cmdidx == CMD_lgetfile 4039 || eap->cmdidx == CMD_laddfile) 4040 wp = curwin; 4041 4042 #ifdef FEAT_AUTOCMD 4043 switch (eap->cmdidx) 4044 { 4045 case CMD_cfile: au_name = (char_u *)"cfile"; break; 4046 case CMD_cgetfile: au_name = (char_u *)"cgetfile"; break; 4047 case CMD_caddfile: au_name = (char_u *)"caddfile"; break; 4048 case CMD_lfile: au_name = (char_u *)"lfile"; break; 4049 case CMD_lgetfile: au_name = (char_u *)"lgetfile"; break; 4050 case CMD_laddfile: au_name = (char_u *)"laddfile"; break; 4051 default: break; 4052 } 4053 if (au_name != NULL) 4054 apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, NULL, FALSE, curbuf); 4055 #endif 4056 #ifdef FEAT_MBYTE 4057 enc = (*curbuf->b_p_menc != NUL) ? curbuf->b_p_menc : p_menc; 4058 #endif 4059 #ifdef FEAT_BROWSE 4060 if (cmdmod.browse) 4061 { 4062 char_u *browse_file = do_browse(0, (char_u *)_("Error file"), eap->arg, 4063 NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL); 4064 if (browse_file == NULL) 4065 return; 4066 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0); 4067 vim_free(browse_file); 4068 } 4069 else 4070 #endif 4071 if (*eap->arg != NUL) 4072 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0); 4073 4074 /* 4075 * This function is used by the :cfile, :cgetfile and :caddfile 4076 * commands. 4077 * :cfile always creates a new quickfix list and jumps to the 4078 * first error. 4079 * :cgetfile creates a new quickfix list but doesn't jump to the 4080 * first error. 4081 * :caddfile adds to an existing quickfix list. If there is no 4082 * quickfix list then a new list is created. 4083 */ 4084 if (qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile 4085 && eap->cmdidx != CMD_laddfile), 4086 *eap->cmdlinep, enc) > 0 4087 && (eap->cmdidx == CMD_cfile 4088 || eap->cmdidx == CMD_lfile)) 4089 { 4090 #ifdef FEAT_AUTOCMD 4091 if (au_name != NULL) 4092 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); 4093 #endif 4094 if (wp != NULL) 4095 qi = GET_LOC_LIST(wp); 4096 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 4097 } 4098 4099 else 4100 { 4101 #ifdef FEAT_AUTOCMD 4102 if (au_name != NULL) 4103 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); 4104 #endif 4105 } 4106 } 4107 4108 /* 4109 * ":vimgrep {pattern} file(s)" 4110 * ":vimgrepadd {pattern} file(s)" 4111 * ":lvimgrep {pattern} file(s)" 4112 * ":lvimgrepadd {pattern} file(s)" 4113 */ 4114 void 4115 ex_vimgrep(exarg_T *eap) 4116 { 4117 regmmatch_T regmatch; 4118 int fcount; 4119 char_u **fnames; 4120 char_u *fname; 4121 char_u *title; 4122 char_u *s; 4123 char_u *p; 4124 int fi; 4125 qf_info_T *qi = &ql_info; 4126 #ifdef FEAT_AUTOCMD 4127 qfline_T *cur_qf_start; 4128 #endif 4129 long lnum; 4130 buf_T *buf; 4131 int duplicate_name = FALSE; 4132 int using_dummy; 4133 int redraw_for_dummy = FALSE; 4134 int found_match; 4135 buf_T *first_match_buf = NULL; 4136 time_t seconds = 0; 4137 int save_mls; 4138 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4139 char_u *save_ei = NULL; 4140 #endif 4141 aco_save_T aco; 4142 int flags = 0; 4143 colnr_T col; 4144 long tomatch; 4145 char_u *dirname_start = NULL; 4146 char_u *dirname_now = NULL; 4147 char_u *target_dir = NULL; 4148 #ifdef FEAT_AUTOCMD 4149 char_u *au_name = NULL; 4150 4151 switch (eap->cmdidx) 4152 { 4153 case CMD_vimgrep: au_name = (char_u *)"vimgrep"; break; 4154 case CMD_lvimgrep: au_name = (char_u *)"lvimgrep"; break; 4155 case CMD_vimgrepadd: au_name = (char_u *)"vimgrepadd"; break; 4156 case CMD_lvimgrepadd: au_name = (char_u *)"lvimgrepadd"; break; 4157 case CMD_grep: au_name = (char_u *)"grep"; break; 4158 case CMD_lgrep: au_name = (char_u *)"lgrep"; break; 4159 case CMD_grepadd: au_name = (char_u *)"grepadd"; break; 4160 case CMD_lgrepadd: au_name = (char_u *)"lgrepadd"; break; 4161 default: break; 4162 } 4163 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 4164 curbuf->b_fname, TRUE, curbuf)) 4165 { 4166 # ifdef FEAT_EVAL 4167 if (aborting()) 4168 return; 4169 # endif 4170 } 4171 #endif 4172 4173 if (eap->cmdidx == CMD_lgrep 4174 || eap->cmdidx == CMD_lvimgrep 4175 || eap->cmdidx == CMD_lgrepadd 4176 || eap->cmdidx == CMD_lvimgrepadd) 4177 { 4178 qi = ll_get_or_alloc_list(curwin); 4179 if (qi == NULL) 4180 return; 4181 } 4182 4183 if (eap->addr_count > 0) 4184 tomatch = eap->line2; 4185 else 4186 tomatch = MAXLNUM; 4187 4188 /* Get the search pattern: either white-separated or enclosed in // */ 4189 regmatch.regprog = NULL; 4190 title = vim_strsave(*eap->cmdlinep); 4191 p = skip_vimgrep_pat(eap->arg, &s, &flags); 4192 if (p == NULL) 4193 { 4194 EMSG(_(e_invalpat)); 4195 goto theend; 4196 } 4197 4198 if (s != NULL && *s == NUL) 4199 { 4200 /* Pattern is empty, use last search pattern. */ 4201 if (last_search_pat() == NULL) 4202 { 4203 EMSG(_(e_noprevre)); 4204 goto theend; 4205 } 4206 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); 4207 } 4208 else 4209 regmatch.regprog = vim_regcomp(s, RE_MAGIC); 4210 4211 if (regmatch.regprog == NULL) 4212 goto theend; 4213 regmatch.rmm_ic = p_ic; 4214 regmatch.rmm_maxcol = 0; 4215 4216 p = skipwhite(p); 4217 if (*p == NUL) 4218 { 4219 EMSG(_("E683: File name missing or invalid pattern")); 4220 goto theend; 4221 } 4222 4223 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd 4224 && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd) 4225 || qi->qf_curlist == qi->qf_listcount) 4226 /* make place for a new list */ 4227 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep); 4228 4229 /* parse the list of arguments */ 4230 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL) 4231 goto theend; 4232 if (fcount == 0) 4233 { 4234 EMSG(_(e_nomatch)); 4235 goto theend; 4236 } 4237 4238 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start); 4239 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now); 4240 if (dirname_start == NULL || dirname_now == NULL) 4241 { 4242 FreeWild(fcount, fnames); 4243 goto theend; 4244 } 4245 4246 /* Remember the current directory, because a BufRead autocommand that does 4247 * ":lcd %:p:h" changes the meaning of short path names. */ 4248 mch_dirname(dirname_start, MAXPATHL); 4249 4250 #ifdef FEAT_AUTOCMD 4251 /* Remember the value of qf_start, so that we can check for autocommands 4252 * changing the current quickfix list. */ 4253 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 4254 #endif 4255 4256 seconds = (time_t)0; 4257 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi) 4258 { 4259 fname = shorten_fname1(fnames[fi]); 4260 if (time(NULL) > seconds) 4261 { 4262 /* Display the file name every second or so, show the user we are 4263 * working on it. */ 4264 seconds = time(NULL); 4265 msg_start(); 4266 p = msg_strtrunc(fname, TRUE); 4267 if (p == NULL) 4268 msg_outtrans(fname); 4269 else 4270 { 4271 msg_outtrans(p); 4272 vim_free(p); 4273 } 4274 msg_clr_eos(); 4275 msg_didout = FALSE; /* overwrite this message */ 4276 msg_nowait = TRUE; /* don't wait for this message */ 4277 msg_col = 0; 4278 out_flush(); 4279 } 4280 4281 buf = buflist_findname_exp(fnames[fi]); 4282 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 4283 { 4284 /* Remember that a buffer with this name already exists. */ 4285 duplicate_name = (buf != NULL); 4286 using_dummy = TRUE; 4287 redraw_for_dummy = TRUE; 4288 4289 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4290 /* Don't do Filetype autocommands to avoid loading syntax and 4291 * indent scripts, a great speed improvement. */ 4292 save_ei = au_event_disable(",Filetype"); 4293 #endif 4294 /* Don't use modelines here, it's useless. */ 4295 save_mls = p_mls; 4296 p_mls = 0; 4297 4298 /* Load file into a buffer, so that 'fileencoding' is detected, 4299 * autocommands applied, etc. */ 4300 buf = load_dummy_buffer(fname, dirname_start, dirname_now); 4301 4302 p_mls = save_mls; 4303 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4304 au_event_restore(save_ei); 4305 #endif 4306 } 4307 else 4308 /* Use existing, loaded buffer. */ 4309 using_dummy = FALSE; 4310 4311 #ifdef FEAT_AUTOCMD 4312 if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start) 4313 { 4314 int idx; 4315 4316 /* Autocommands changed the quickfix list. Find the one we were 4317 * using and restore it. */ 4318 for (idx = 0; idx < LISTCOUNT; ++idx) 4319 if (cur_qf_start == qi->qf_lists[idx].qf_start) 4320 { 4321 qi->qf_curlist = idx; 4322 break; 4323 } 4324 if (idx == LISTCOUNT) 4325 { 4326 /* List cannot be found, create a new one. */ 4327 qf_new_list(qi, *eap->cmdlinep); 4328 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 4329 } 4330 } 4331 #endif 4332 4333 if (buf == NULL) 4334 { 4335 if (!got_int) 4336 smsg((char_u *)_("Cannot open file \"%s\""), fname); 4337 } 4338 else 4339 { 4340 /* Try for a match in all lines of the buffer. 4341 * For ":1vimgrep" look for first match only. */ 4342 found_match = FALSE; 4343 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; 4344 ++lnum) 4345 { 4346 col = 0; 4347 while (vim_regexec_multi(®match, curwin, buf, lnum, 4348 col, NULL, NULL) > 0) 4349 { 4350 /* Pass the buffer number so that it gets used even for a 4351 * dummy buffer, unless duplicate_name is set, then the 4352 * buffer will be wiped out below. */ 4353 if (qf_add_entry(qi, 4354 qi->qf_curlist, 4355 NULL, /* dir */ 4356 fname, 4357 duplicate_name ? 0 : buf->b_fnum, 4358 ml_get_buf(buf, 4359 regmatch.startpos[0].lnum + lnum, FALSE), 4360 regmatch.startpos[0].lnum + lnum, 4361 regmatch.startpos[0].col + 1, 4362 FALSE, /* vis_col */ 4363 NULL, /* search pattern */ 4364 0, /* nr */ 4365 0, /* type */ 4366 TRUE /* valid */ 4367 ) == FAIL) 4368 { 4369 got_int = TRUE; 4370 break; 4371 } 4372 found_match = TRUE; 4373 if (--tomatch == 0) 4374 break; 4375 if ((flags & VGR_GLOBAL) == 0 4376 || regmatch.endpos[0].lnum > 0) 4377 break; 4378 col = regmatch.endpos[0].col 4379 + (col == regmatch.endpos[0].col); 4380 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE))) 4381 break; 4382 } 4383 line_breakcheck(); 4384 if (got_int) 4385 break; 4386 } 4387 #ifdef FEAT_AUTOCMD 4388 cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start; 4389 #endif 4390 4391 if (using_dummy) 4392 { 4393 if (found_match && first_match_buf == NULL) 4394 first_match_buf = buf; 4395 if (duplicate_name) 4396 { 4397 /* Never keep a dummy buffer if there is another buffer 4398 * with the same name. */ 4399 wipe_dummy_buffer(buf, dirname_start); 4400 buf = NULL; 4401 } 4402 else if (!cmdmod.hide 4403 || buf->b_p_bh[0] == 'u' /* "unload" */ 4404 || buf->b_p_bh[0] == 'w' /* "wipe" */ 4405 || buf->b_p_bh[0] == 'd') /* "delete" */ 4406 { 4407 /* When no match was found we don't need to remember the 4408 * buffer, wipe it out. If there was a match and it 4409 * wasn't the first one or we won't jump there: only 4410 * unload the buffer. 4411 * Ignore 'hidden' here, because it may lead to having too 4412 * many swap files. */ 4413 if (!found_match) 4414 { 4415 wipe_dummy_buffer(buf, dirname_start); 4416 buf = NULL; 4417 } 4418 else if (buf != first_match_buf || (flags & VGR_NOJUMP)) 4419 { 4420 unload_dummy_buffer(buf, dirname_start); 4421 /* Keeping the buffer, remove the dummy flag. */ 4422 buf->b_flags &= ~BF_DUMMY; 4423 buf = NULL; 4424 } 4425 } 4426 4427 if (buf != NULL) 4428 { 4429 /* Keeping the buffer, remove the dummy flag. */ 4430 buf->b_flags &= ~BF_DUMMY; 4431 4432 /* If the buffer is still loaded we need to use the 4433 * directory we jumped to below. */ 4434 if (buf == first_match_buf 4435 && target_dir == NULL 4436 && STRCMP(dirname_start, dirname_now) != 0) 4437 target_dir = vim_strsave(dirname_now); 4438 4439 /* The buffer is still loaded, the Filetype autocommands 4440 * need to be done now, in that buffer. And the modelines 4441 * need to be done (again). But not the window-local 4442 * options! */ 4443 aucmd_prepbuf(&aco, buf); 4444 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) 4445 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, 4446 buf->b_fname, TRUE, buf); 4447 #endif 4448 do_modelines(OPT_NOWIN); 4449 aucmd_restbuf(&aco); 4450 } 4451 } 4452 } 4453 } 4454 4455 FreeWild(fcount, fnames); 4456 4457 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 4458 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start; 4459 qi->qf_lists[qi->qf_curlist].qf_index = 1; 4460 4461 qf_update_buffer(qi, NULL); 4462 4463 #ifdef FEAT_AUTOCMD 4464 if (au_name != NULL) 4465 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 4466 curbuf->b_fname, TRUE, curbuf); 4467 #endif 4468 4469 /* Jump to first match. */ 4470 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 4471 { 4472 if ((flags & VGR_NOJUMP) == 0) 4473 { 4474 buf = curbuf; 4475 qf_jump(qi, 0, 0, eap->forceit); 4476 if (buf != curbuf) 4477 /* If we jumped to another buffer redrawing will already be 4478 * taken care of. */ 4479 redraw_for_dummy = FALSE; 4480 4481 /* Jump to the directory used after loading the buffer. */ 4482 if (curbuf == first_match_buf && target_dir != NULL) 4483 { 4484 exarg_T ea; 4485 4486 ea.arg = target_dir; 4487 ea.cmdidx = CMD_lcd; 4488 ex_cd(&ea); 4489 } 4490 } 4491 } 4492 else 4493 EMSG2(_(e_nomatch2), s); 4494 4495 /* If we loaded a dummy buffer into the current window, the autocommands 4496 * may have messed up things, need to redraw and recompute folds. */ 4497 if (redraw_for_dummy) 4498 { 4499 #ifdef FEAT_FOLDING 4500 foldUpdateAll(curwin); 4501 #else 4502 redraw_later(NOT_VALID); 4503 #endif 4504 } 4505 4506 theend: 4507 vim_free(title); 4508 vim_free(dirname_now); 4509 vim_free(dirname_start); 4510 vim_free(target_dir); 4511 vim_regfree(regmatch.regprog); 4512 } 4513 4514 /* 4515 * Restore current working directory to "dirname_start" if they differ, taking 4516 * into account whether it is set locally or globally. 4517 */ 4518 static void 4519 restore_start_dir(char_u *dirname_start) 4520 { 4521 char_u *dirname_now = alloc(MAXPATHL); 4522 4523 if (NULL != dirname_now) 4524 { 4525 mch_dirname(dirname_now, MAXPATHL); 4526 if (STRCMP(dirname_start, dirname_now) != 0) 4527 { 4528 /* If the directory has changed, change it back by building up an 4529 * appropriate ex command and executing it. */ 4530 exarg_T ea; 4531 4532 ea.arg = dirname_start; 4533 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd; 4534 ex_cd(&ea); 4535 } 4536 vim_free(dirname_now); 4537 } 4538 } 4539 4540 /* 4541 * Load file "fname" into a dummy buffer and return the buffer pointer, 4542 * placing the directory resulting from the buffer load into the 4543 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller 4544 * prior to calling this function. Restores directory to "dirname_start" prior 4545 * to returning, if autocmds or the 'autochdir' option have changed it. 4546 * 4547 * If creating the dummy buffer does not fail, must call unload_dummy_buffer() 4548 * or wipe_dummy_buffer() later! 4549 * 4550 * Returns NULL if it fails. 4551 */ 4552 static buf_T * 4553 load_dummy_buffer( 4554 char_u *fname, 4555 char_u *dirname_start, /* in: old directory */ 4556 char_u *resulting_dir) /* out: new directory */ 4557 { 4558 buf_T *newbuf; 4559 bufref_T newbufref; 4560 bufref_T newbuf_to_wipe; 4561 int failed = TRUE; 4562 aco_save_T aco; 4563 4564 /* Allocate a buffer without putting it in the buffer list. */ 4565 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); 4566 if (newbuf == NULL) 4567 return NULL; 4568 set_bufref(&newbufref, newbuf); 4569 4570 /* Init the options. */ 4571 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); 4572 4573 /* need to open the memfile before putting the buffer in a window */ 4574 if (ml_open(newbuf) == OK) 4575 { 4576 /* set curwin/curbuf to buf and save a few things */ 4577 aucmd_prepbuf(&aco, newbuf); 4578 4579 /* Need to set the filename for autocommands. */ 4580 (void)setfname(curbuf, fname, NULL, FALSE); 4581 4582 /* Create swap file now to avoid the ATTENTION message. */ 4583 check_need_swap(TRUE); 4584 4585 /* Remove the "dummy" flag, otherwise autocommands may not 4586 * work. */ 4587 curbuf->b_flags &= ~BF_DUMMY; 4588 4589 newbuf_to_wipe.br_buf = NULL; 4590 if (readfile(fname, NULL, 4591 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, 4592 NULL, READ_NEW | READ_DUMMY) == OK 4593 && !got_int 4594 && !(curbuf->b_flags & BF_NEW)) 4595 { 4596 failed = FALSE; 4597 if (curbuf != newbuf) 4598 { 4599 /* Bloody autocommands changed the buffer! Can happen when 4600 * using netrw and editing a remote file. Use the current 4601 * buffer instead, delete the dummy one after restoring the 4602 * window stuff. */ 4603 set_bufref(&newbuf_to_wipe, newbuf); 4604 newbuf = curbuf; 4605 } 4606 } 4607 4608 /* restore curwin/curbuf and a few other things */ 4609 aucmd_restbuf(&aco); 4610 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) 4611 wipe_buffer(newbuf_to_wipe.br_buf, FALSE); 4612 4613 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't 4614 * skip it. */ 4615 newbuf->b_flags |= BF_DUMMY; 4616 } 4617 4618 /* 4619 * When autocommands/'autochdir' option changed directory: go back. 4620 * Let the caller know what the resulting dir was first, in case it is 4621 * important. 4622 */ 4623 mch_dirname(resulting_dir, MAXPATHL); 4624 restore_start_dir(dirname_start); 4625 4626 if (!bufref_valid(&newbufref)) 4627 return NULL; 4628 if (failed) 4629 { 4630 wipe_dummy_buffer(newbuf, dirname_start); 4631 return NULL; 4632 } 4633 return newbuf; 4634 } 4635 4636 /* 4637 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores 4638 * directory to "dirname_start" prior to returning, if autocmds or the 4639 * 'autochdir' option have changed it. 4640 */ 4641 static void 4642 wipe_dummy_buffer(buf_T *buf, char_u *dirname_start) 4643 { 4644 if (curbuf != buf) /* safety check */ 4645 { 4646 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4647 cleanup_T cs; 4648 4649 /* Reset the error/interrupt/exception state here so that aborting() 4650 * returns FALSE when wiping out the buffer. Otherwise it doesn't 4651 * work when got_int is set. */ 4652 enter_cleanup(&cs); 4653 #endif 4654 4655 wipe_buffer(buf, FALSE); 4656 4657 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4658 /* Restore the error/interrupt/exception state if not discarded by a 4659 * new aborting error, interrupt, or uncaught exception. */ 4660 leave_cleanup(&cs); 4661 #endif 4662 /* When autocommands/'autochdir' option changed directory: go back. */ 4663 restore_start_dir(dirname_start); 4664 } 4665 } 4666 4667 /* 4668 * Unload the dummy buffer that load_dummy_buffer() created. Restores 4669 * directory to "dirname_start" prior to returning, if autocmds or the 4670 * 'autochdir' option have changed it. 4671 */ 4672 static void 4673 unload_dummy_buffer(buf_T *buf, char_u *dirname_start) 4674 { 4675 if (curbuf != buf) /* safety check */ 4676 { 4677 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE); 4678 4679 /* When autocommands/'autochdir' option changed directory: go back. */ 4680 restore_start_dir(dirname_start); 4681 } 4682 } 4683 4684 #if defined(FEAT_EVAL) || defined(PROTO) 4685 /* 4686 * Add each quickfix error to list "list" as a dictionary. 4687 * If qf_idx is -1, use the current list. Otherwise, use the specified list. 4688 */ 4689 int 4690 get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list) 4691 { 4692 qf_info_T *qi = qi_arg; 4693 dict_T *dict; 4694 char_u buf[2]; 4695 qfline_T *qfp; 4696 int i; 4697 int bufnum; 4698 4699 if (qi == NULL) 4700 { 4701 qi = &ql_info; 4702 if (wp != NULL) 4703 { 4704 qi = GET_LOC_LIST(wp); 4705 if (qi == NULL) 4706 return FAIL; 4707 } 4708 } 4709 4710 if (qf_idx == -1) 4711 qf_idx = qi->qf_curlist; 4712 4713 if (qf_idx >= qi->qf_listcount 4714 || qi->qf_lists[qf_idx].qf_count == 0) 4715 return FAIL; 4716 4717 qfp = qi->qf_lists[qf_idx].qf_start; 4718 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i) 4719 { 4720 /* Handle entries with a non-existing buffer number. */ 4721 bufnum = qfp->qf_fnum; 4722 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 4723 bufnum = 0; 4724 4725 if ((dict = dict_alloc()) == NULL) 4726 return FAIL; 4727 if (list_append_dict(list, dict) == FAIL) 4728 return FAIL; 4729 4730 buf[0] = qfp->qf_type; 4731 buf[1] = NUL; 4732 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL 4733 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL 4734 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL 4735 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL 4736 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL 4737 || dict_add_nr_str(dict, "pattern", 0L, 4738 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL 4739 || dict_add_nr_str(dict, "text", 0L, 4740 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL 4741 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL 4742 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL) 4743 return FAIL; 4744 4745 qfp = qfp->qf_next; 4746 if (qfp == NULL) 4747 break; 4748 } 4749 return OK; 4750 } 4751 4752 /* 4753 * Flags used by getqflist()/getloclist() to determine which fields to return. 4754 */ 4755 enum { 4756 QF_GETLIST_NONE = 0x0, 4757 QF_GETLIST_TITLE = 0x1, 4758 QF_GETLIST_ITEMS = 0x2, 4759 QF_GETLIST_NR = 0x4, 4760 QF_GETLIST_WINID = 0x8, 4761 QF_GETLIST_CONTEXT = 0x10, 4762 QF_GETLIST_ID = 0x20, 4763 QF_GETLIST_IDX = 0x40, 4764 QF_GETLIST_SIZE = 0x80, 4765 QF_GETLIST_ALL = 0xFF 4766 }; 4767 4768 /* 4769 * Parse text from 'di' and return the quickfix list items 4770 */ 4771 static int 4772 qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict) 4773 { 4774 int status = FAIL; 4775 qf_info_T *qi; 4776 char_u *errorformat = p_efm; 4777 dictitem_T *efm_di; 4778 list_T *l; 4779 4780 /* Only a List value is supported */ 4781 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL) 4782 { 4783 /* If errorformat is supplied then use it, otherwise use the 'efm' 4784 * option setting 4785 */ 4786 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL) 4787 { 4788 if (efm_di->di_tv.v_type != VAR_STRING || 4789 efm_di->di_tv.vval.v_string == NULL) 4790 return FAIL; 4791 errorformat = efm_di->di_tv.vval.v_string; 4792 } 4793 4794 l = list_alloc(); 4795 if (l == NULL) 4796 return FAIL; 4797 4798 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T)); 4799 if (qi != NULL) 4800 { 4801 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T))); 4802 qi->qf_refcount++; 4803 4804 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat, 4805 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0) 4806 { 4807 (void)get_errorlist(qi, NULL, 0, l); 4808 qf_free(qi, 0); 4809 } 4810 free(qi); 4811 } 4812 dict_add_list(retdict, "items", l); 4813 status = OK; 4814 } 4815 4816 return status; 4817 } 4818 4819 /* 4820 * Return the quickfix/location list number with the given identifier. 4821 * Returns -1 if list is not found. 4822 */ 4823 static int 4824 qf_id2nr(qf_info_T *qi, int_u qfid) 4825 { 4826 int qf_idx; 4827 4828 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++) 4829 if (qi->qf_lists[qf_idx].qf_id == qfid) 4830 return qf_idx; 4831 return -1; 4832 } 4833 4834 /* 4835 * Return quickfix/location list details (title) as a 4836 * dictionary. 'what' contains the details to return. If 'list_idx' is -1, 4837 * then current list is used. Otherwise the specified list is used. 4838 */ 4839 int 4840 qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) 4841 { 4842 qf_info_T *qi = &ql_info; 4843 int status = OK; 4844 int qf_idx; 4845 dictitem_T *di; 4846 int flags = QF_GETLIST_NONE; 4847 4848 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL) 4849 return qf_get_list_from_lines(what, di, retdict); 4850 4851 if (wp != NULL) 4852 qi = GET_LOC_LIST(wp); 4853 4854 /* List is not present or is empty */ 4855 if (qi == NULL || qi->qf_listcount == 0) 4856 { 4857 /* If querying for the size of the list, return 0 */ 4858 if (((di = dict_find(what, (char_u *)"nr", -1)) != NULL) 4859 && (di->di_tv.v_type == VAR_STRING) 4860 && (STRCMP(di->di_tv.vval.v_string, "$") == 0)) 4861 return dict_add_nr_str(retdict, "nr", 0, NULL); 4862 return FAIL; 4863 } 4864 4865 qf_idx = qi->qf_curlist; /* default is the current list */ 4866 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL) 4867 { 4868 /* Use the specified quickfix/location list */ 4869 if (di->di_tv.v_type == VAR_NUMBER) 4870 { 4871 /* for zero use the current list */ 4872 if (di->di_tv.vval.v_number != 0) 4873 { 4874 qf_idx = di->di_tv.vval.v_number - 1; 4875 if (qf_idx < 0 || qf_idx >= qi->qf_listcount) 4876 return FAIL; 4877 } 4878 } 4879 else if ((di->di_tv.v_type == VAR_STRING) 4880 && (STRCMP(di->di_tv.vval.v_string, "$") == 0)) 4881 /* Get the last quickfix list number */ 4882 qf_idx = qi->qf_listcount - 1; 4883 else 4884 return FAIL; 4885 flags |= QF_GETLIST_NR; 4886 } 4887 4888 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL) 4889 { 4890 /* Look for a list with the specified id */ 4891 if (di->di_tv.v_type == VAR_NUMBER) 4892 { 4893 /* For zero, use the current list or the list specifed by 'nr' */ 4894 if (di->di_tv.vval.v_number != 0) 4895 { 4896 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number); 4897 if (qf_idx == -1) 4898 return FAIL; /* List not found */ 4899 } 4900 flags |= QF_GETLIST_ID; 4901 } 4902 else 4903 return FAIL; 4904 } 4905 4906 if (dict_find(what, (char_u *)"all", -1) != NULL) 4907 flags |= QF_GETLIST_ALL; 4908 4909 if (dict_find(what, (char_u *)"title", -1) != NULL) 4910 flags |= QF_GETLIST_TITLE; 4911 4912 if (dict_find(what, (char_u *)"winid", -1) != NULL) 4913 flags |= QF_GETLIST_WINID; 4914 4915 if (dict_find(what, (char_u *)"context", -1) != NULL) 4916 flags |= QF_GETLIST_CONTEXT; 4917 4918 if (dict_find(what, (char_u *)"items", -1) != NULL) 4919 flags |= QF_GETLIST_ITEMS; 4920 4921 if (dict_find(what, (char_u *)"idx", -1) != NULL) 4922 flags |= QF_GETLIST_IDX; 4923 4924 if (dict_find(what, (char_u *)"size", -1) != NULL) 4925 flags |= QF_GETLIST_SIZE; 4926 4927 if (flags & QF_GETLIST_TITLE) 4928 { 4929 char_u *t; 4930 t = qi->qf_lists[qf_idx].qf_title; 4931 if (t == NULL) 4932 t = (char_u *)""; 4933 status = dict_add_nr_str(retdict, "title", 0L, t); 4934 } 4935 if ((status == OK) && (flags & QF_GETLIST_NR)) 4936 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL); 4937 if ((status == OK) && (flags & QF_GETLIST_WINID)) 4938 { 4939 win_T *win; 4940 win = qf_find_win(qi); 4941 if (win != NULL) 4942 status = dict_add_nr_str(retdict, "winid", win->w_id, NULL); 4943 } 4944 if ((status == OK) && (flags & QF_GETLIST_ITEMS)) 4945 { 4946 list_T *l = list_alloc(); 4947 if (l != NULL) 4948 { 4949 (void)get_errorlist(qi, NULL, qf_idx, l); 4950 dict_add_list(retdict, "items", l); 4951 } 4952 else 4953 status = FAIL; 4954 } 4955 4956 if ((status == OK) && (flags & QF_GETLIST_CONTEXT)) 4957 { 4958 if (qi->qf_lists[qf_idx].qf_ctx != NULL) 4959 { 4960 di = dictitem_alloc((char_u *)"context"); 4961 if (di != NULL) 4962 { 4963 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv); 4964 status = dict_add(retdict, di); 4965 if (status == FAIL) 4966 dictitem_free(di); 4967 } 4968 else 4969 status = FAIL; 4970 } 4971 else 4972 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)""); 4973 } 4974 4975 if ((status == OK) && (flags & QF_GETLIST_ID)) 4976 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id, 4977 NULL); 4978 4979 if ((status == OK) && (flags & QF_GETLIST_IDX)) 4980 { 4981 int idx = qi->qf_lists[qf_idx].qf_index; 4982 if (qi->qf_lists[qf_idx].qf_count == 0) 4983 /* For empty lists, qf_index is set to 1 */ 4984 idx = 0; 4985 status = dict_add_nr_str(retdict, "idx", idx, NULL); 4986 } 4987 4988 if ((status == OK) && (flags & QF_GETLIST_SIZE)) 4989 status = dict_add_nr_str(retdict, "size", 4990 qi->qf_lists[qf_idx].qf_count, NULL); 4991 4992 return status; 4993 } 4994 4995 /* 4996 * Add list of entries to quickfix/location list. Each list entry is 4997 * a dictionary with item information. 4998 */ 4999 static int 5000 qf_add_entries( 5001 qf_info_T *qi, 5002 int qf_idx, 5003 list_T *list, 5004 char_u *title, 5005 int action) 5006 { 5007 listitem_T *li; 5008 dict_T *d; 5009 char_u *filename, *pattern, *text, *type; 5010 int bufnum; 5011 long lnum; 5012 int col, nr; 5013 int vcol; 5014 qfline_T *old_last = NULL; 5015 int valid, status; 5016 int retval = OK; 5017 int did_bufnr_emsg = FALSE; 5018 5019 if (action == ' ' || qf_idx == qi->qf_listcount) 5020 { 5021 /* make place for a new list */ 5022 qf_new_list(qi, title); 5023 qf_idx = qi->qf_curlist; 5024 } 5025 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0) 5026 /* Adding to existing list, use last entry. */ 5027 old_last = qi->qf_lists[qf_idx].qf_last; 5028 else if (action == 'r') 5029 { 5030 qf_free_items(qi, qf_idx); 5031 qf_store_title(qi, qf_idx, title); 5032 } 5033 5034 for (li = list->lv_first; li != NULL; li = li->li_next) 5035 { 5036 if (li->li_tv.v_type != VAR_DICT) 5037 continue; /* Skip non-dict items */ 5038 5039 d = li->li_tv.vval.v_dict; 5040 if (d == NULL) 5041 continue; 5042 5043 filename = get_dict_string(d, (char_u *)"filename", TRUE); 5044 bufnum = (int)get_dict_number(d, (char_u *)"bufnr"); 5045 lnum = (int)get_dict_number(d, (char_u *)"lnum"); 5046 col = (int)get_dict_number(d, (char_u *)"col"); 5047 vcol = (int)get_dict_number(d, (char_u *)"vcol"); 5048 nr = (int)get_dict_number(d, (char_u *)"nr"); 5049 type = get_dict_string(d, (char_u *)"type", TRUE); 5050 pattern = get_dict_string(d, (char_u *)"pattern", TRUE); 5051 text = get_dict_string(d, (char_u *)"text", TRUE); 5052 if (text == NULL) 5053 text = vim_strsave((char_u *)""); 5054 5055 valid = TRUE; 5056 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL)) 5057 valid = FALSE; 5058 5059 /* Mark entries with non-existing buffer number as not valid. Give the 5060 * error message only once. */ 5061 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 5062 { 5063 if (!did_bufnr_emsg) 5064 { 5065 did_bufnr_emsg = TRUE; 5066 EMSGN(_("E92: Buffer %ld not found"), bufnum); 5067 } 5068 valid = FALSE; 5069 bufnum = 0; 5070 } 5071 5072 /* If the 'valid' field is present it overrules the detected value. */ 5073 if ((dict_find(d, (char_u *)"valid", -1)) != NULL) 5074 valid = (int)get_dict_number(d, (char_u *)"valid"); 5075 5076 status = qf_add_entry(qi, 5077 qf_idx, 5078 NULL, /* dir */ 5079 filename, 5080 bufnum, 5081 text, 5082 lnum, 5083 col, 5084 vcol, /* vis_col */ 5085 pattern, /* search pattern */ 5086 nr, 5087 type == NULL ? NUL : *type, 5088 valid); 5089 5090 vim_free(filename); 5091 vim_free(pattern); 5092 vim_free(text); 5093 vim_free(type); 5094 5095 if (status == FAIL) 5096 { 5097 retval = FAIL; 5098 break; 5099 } 5100 } 5101 5102 if (qi->qf_lists[qf_idx].qf_index == 0) 5103 /* no valid entry */ 5104 qi->qf_lists[qf_idx].qf_nonevalid = TRUE; 5105 else 5106 qi->qf_lists[qf_idx].qf_nonevalid = FALSE; 5107 if (action != 'a') 5108 { 5109 qi->qf_lists[qf_idx].qf_ptr = 5110 qi->qf_lists[qf_idx].qf_start; 5111 if (qi->qf_lists[qf_idx].qf_count > 0) 5112 qi->qf_lists[qf_idx].qf_index = 1; 5113 } 5114 5115 /* Don't update the cursor in quickfix window when appending entries */ 5116 qf_update_buffer(qi, old_last); 5117 5118 return retval; 5119 } 5120 5121 static int 5122 qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title) 5123 { 5124 dictitem_T *di; 5125 int retval = FAIL; 5126 int qf_idx; 5127 int newlist = FALSE; 5128 char_u *errorformat = p_efm; 5129 5130 if (action == ' ' || qi->qf_curlist == qi->qf_listcount) 5131 newlist = TRUE; 5132 5133 qf_idx = qi->qf_curlist; /* default is the current list */ 5134 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL) 5135 { 5136 /* Use the specified quickfix/location list */ 5137 if (di->di_tv.v_type == VAR_NUMBER) 5138 { 5139 /* for zero use the current list */ 5140 if (di->di_tv.vval.v_number != 0) 5141 qf_idx = di->di_tv.vval.v_number - 1; 5142 5143 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount) 5144 { 5145 /* 5146 * When creating a new list, accept qf_idx pointing to the next 5147 * non-available list and add the new list at the end of the 5148 * stack. 5149 */ 5150 newlist = TRUE; 5151 qf_idx = qi->qf_listcount - 1; 5152 } 5153 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount) 5154 return FAIL; 5155 else if (action != ' ') 5156 newlist = FALSE; /* use the specified list */ 5157 } 5158 else if (di->di_tv.v_type == VAR_STRING 5159 && STRCMP(di->di_tv.vval.v_string, "$") == 0) 5160 { 5161 if (qi->qf_listcount > 0) 5162 qf_idx = qi->qf_listcount - 1; 5163 else if (newlist) 5164 qf_idx = 0; 5165 else 5166 return FAIL; 5167 } 5168 else 5169 return FAIL; 5170 } 5171 5172 if (!newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL) 5173 { 5174 /* Use the quickfix/location list with the specified id */ 5175 if (di->di_tv.v_type == VAR_NUMBER) 5176 { 5177 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number); 5178 if (qf_idx == -1) 5179 return FAIL; /* List not found */ 5180 } 5181 else 5182 return FAIL; 5183 } 5184 5185 if (newlist) 5186 { 5187 qi->qf_curlist = qf_idx; 5188 qf_new_list(qi, title); 5189 qf_idx = qi->qf_curlist; 5190 } 5191 5192 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL) 5193 { 5194 if (di->di_tv.v_type == VAR_STRING) 5195 { 5196 vim_free(qi->qf_lists[qf_idx].qf_title); 5197 qi->qf_lists[qf_idx].qf_title = 5198 get_dict_string(what, (char_u *)"title", TRUE); 5199 if (qf_idx == qi->qf_curlist) 5200 qf_update_win_titlevar(qi); 5201 retval = OK; 5202 } 5203 } 5204 5205 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL) 5206 { 5207 if (di->di_tv.v_type == VAR_LIST) 5208 { 5209 char_u *title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title); 5210 5211 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list, 5212 title_save, action == ' ' ? 'a' : action); 5213 if (action == 'r') 5214 { 5215 /* 5216 * When replacing the quickfix list entries using 5217 * qf_add_entries(), the title is set with a ':' prefix. 5218 * Restore the title with the saved title. 5219 */ 5220 vim_free(qi->qf_lists[qf_idx].qf_title); 5221 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save); 5222 } 5223 vim_free(title_save); 5224 } 5225 } 5226 5227 if ((di = dict_find(what, (char_u *)"efm", -1)) != NULL) 5228 { 5229 if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL) 5230 return FAIL; 5231 errorformat = di->di_tv.vval.v_string; 5232 } 5233 5234 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL) 5235 { 5236 /* Only a List value is supported */ 5237 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL) 5238 { 5239 if (action == 'r') 5240 qf_free_items(qi, qf_idx); 5241 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat, 5242 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0) 5243 retval = OK; 5244 } 5245 else 5246 return FAIL; 5247 } 5248 5249 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL) 5250 { 5251 typval_T *ctx; 5252 5253 free_tv(qi->qf_lists[qf_idx].qf_ctx); 5254 ctx = alloc_tv(); 5255 if (ctx != NULL) 5256 copy_tv(&di->di_tv, ctx); 5257 qi->qf_lists[qf_idx].qf_ctx = ctx; 5258 retval = OK; 5259 } 5260 5261 return retval; 5262 } 5263 5264 /* 5265 * Find the non-location list window with the specified location list. 5266 */ 5267 static win_T * 5268 find_win_with_ll(qf_info_T *qi) 5269 { 5270 win_T *wp = NULL; 5271 5272 FOR_ALL_WINDOWS(wp) 5273 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer)) 5274 return wp; 5275 5276 return NULL; 5277 } 5278 5279 /* 5280 * Free the entire quickfix/location list stack. 5281 * If the quickfix/location list window is open, then clear it. 5282 */ 5283 static void 5284 qf_free_stack(win_T *wp, qf_info_T *qi) 5285 { 5286 win_T *qfwin = qf_find_win(qi); 5287 win_T *llwin = NULL; 5288 win_T *orig_wp = wp; 5289 5290 if (qfwin != NULL) 5291 { 5292 /* If the quickfix/location list window is open, then clear it */ 5293 if (qi->qf_curlist < qi->qf_listcount) 5294 qf_free(qi, qi->qf_curlist); 5295 qf_update_buffer(qi, NULL); 5296 } 5297 5298 if (wp != NULL && IS_LL_WINDOW(wp)) 5299 { 5300 /* If in the location list window, then use the non-location list 5301 * window with this location list (if present) 5302 */ 5303 llwin = find_win_with_ll(qi); 5304 if (llwin != NULL) 5305 wp = llwin; 5306 } 5307 5308 qf_free_all(wp); 5309 if (wp == NULL) 5310 { 5311 /* quickfix list */ 5312 qi->qf_curlist = 0; 5313 qi->qf_listcount = 0; 5314 } 5315 else if (IS_LL_WINDOW(orig_wp)) 5316 { 5317 /* If the location list window is open, then create a new empty 5318 * location list */ 5319 qf_info_T *new_ll = ll_new_list(); 5320 5321 /* first free the list reference in the location list window */ 5322 ll_free_all(&orig_wp->w_llist_ref); 5323 5324 orig_wp->w_llist_ref = new_ll; 5325 if (llwin != NULL) 5326 { 5327 llwin->w_llist = new_ll; 5328 new_ll->qf_refcount++; 5329 } 5330 } 5331 } 5332 5333 /* 5334 * Populate the quickfix list with the items supplied in the list 5335 * of dictionaries. "title" will be copied to w:quickfix_title. 5336 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list. 5337 */ 5338 int 5339 set_errorlist( 5340 win_T *wp, 5341 list_T *list, 5342 int action, 5343 char_u *title, 5344 dict_T *what) 5345 { 5346 qf_info_T *qi = &ql_info; 5347 int retval = OK; 5348 5349 if (wp != NULL) 5350 { 5351 qi = ll_get_or_alloc_list(wp); 5352 if (qi == NULL) 5353 return FAIL; 5354 } 5355 5356 if (action == 'f') 5357 { 5358 /* Free the entire quickfix or location list stack */ 5359 qf_free_stack(wp, qi); 5360 } 5361 else if (what != NULL) 5362 retval = qf_set_properties(qi, what, action, title); 5363 else 5364 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action); 5365 5366 return retval; 5367 } 5368 5369 static int 5370 mark_quickfix_ctx(qf_info_T *qi, int copyID) 5371 { 5372 int i; 5373 int abort = FALSE; 5374 typval_T *ctx; 5375 5376 for (i = 0; i < LISTCOUNT && !abort; ++i) 5377 { 5378 ctx = qi->qf_lists[i].qf_ctx; 5379 if (ctx != NULL && ctx->v_type != VAR_NUMBER 5380 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT) 5381 abort = set_ref_in_item(ctx, copyID, NULL, NULL); 5382 } 5383 5384 return abort; 5385 } 5386 5387 /* 5388 * Mark the context of the quickfix list and the location lists (if present) as 5389 * "in use". So that garabage collection doesn't free the context. 5390 */ 5391 int 5392 set_ref_in_quickfix(int copyID) 5393 { 5394 int abort = FALSE; 5395 tabpage_T *tp; 5396 win_T *win; 5397 5398 abort = mark_quickfix_ctx(&ql_info, copyID); 5399 if (abort) 5400 return abort; 5401 5402 FOR_ALL_TAB_WINDOWS(tp, win) 5403 { 5404 if (win->w_llist != NULL) 5405 { 5406 abort = mark_quickfix_ctx(win->w_llist, copyID); 5407 if (abort) 5408 return abort; 5409 } 5410 } 5411 5412 return abort; 5413 } 5414 #endif 5415 5416 /* 5417 * ":[range]cbuffer [bufnr]" command. 5418 * ":[range]caddbuffer [bufnr]" command. 5419 * ":[range]cgetbuffer [bufnr]" command. 5420 * ":[range]lbuffer [bufnr]" command. 5421 * ":[range]laddbuffer [bufnr]" command. 5422 * ":[range]lgetbuffer [bufnr]" command. 5423 */ 5424 void 5425 ex_cbuffer(exarg_T *eap) 5426 { 5427 buf_T *buf = NULL; 5428 qf_info_T *qi = &ql_info; 5429 #ifdef FEAT_AUTOCMD 5430 char_u *au_name = NULL; 5431 #endif 5432 5433 if (eap->cmdidx == CMD_lbuffer || eap->cmdidx == CMD_lgetbuffer 5434 || eap->cmdidx == CMD_laddbuffer) 5435 { 5436 qi = ll_get_or_alloc_list(curwin); 5437 if (qi == NULL) 5438 return; 5439 } 5440 5441 #ifdef FEAT_AUTOCMD 5442 switch (eap->cmdidx) 5443 { 5444 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break; 5445 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break; 5446 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break; 5447 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break; 5448 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break; 5449 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break; 5450 default: break; 5451 } 5452 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 5453 curbuf->b_fname, TRUE, curbuf)) 5454 { 5455 # ifdef FEAT_EVAL 5456 if (aborting()) 5457 return; 5458 # endif 5459 } 5460 #endif 5461 5462 if (*eap->arg == NUL) 5463 buf = curbuf; 5464 else if (*skipwhite(skipdigits(eap->arg)) == NUL) 5465 buf = buflist_findnr(atoi((char *)eap->arg)); 5466 if (buf == NULL) 5467 EMSG(_(e_invarg)); 5468 else if (buf->b_ml.ml_mfp == NULL) 5469 EMSG(_("E681: Buffer is not loaded")); 5470 else 5471 { 5472 if (eap->addr_count == 0) 5473 { 5474 eap->line1 = 1; 5475 eap->line2 = buf->b_ml.ml_line_count; 5476 } 5477 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count 5478 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count) 5479 EMSG(_(e_invrange)); 5480 else 5481 { 5482 char_u *qf_title = *eap->cmdlinep; 5483 5484 if (buf->b_sfname) 5485 { 5486 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)", 5487 (char *)qf_title, (char *)buf->b_sfname); 5488 qf_title = IObuff; 5489 } 5490 5491 if (qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm, 5492 (eap->cmdidx != CMD_caddbuffer 5493 && eap->cmdidx != CMD_laddbuffer), 5494 eap->line1, eap->line2, 5495 qf_title, NULL) > 0) 5496 { 5497 #ifdef FEAT_AUTOCMD 5498 if (au_name != NULL) 5499 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 5500 curbuf->b_fname, TRUE, curbuf); 5501 #endif 5502 if (eap->cmdidx == CMD_cbuffer || eap->cmdidx == CMD_lbuffer) 5503 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 5504 } 5505 } 5506 } 5507 } 5508 5509 #if defined(FEAT_EVAL) || defined(PROTO) 5510 /* 5511 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command. 5512 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command. 5513 */ 5514 void 5515 ex_cexpr(exarg_T *eap) 5516 { 5517 typval_T *tv; 5518 qf_info_T *qi = &ql_info; 5519 #ifdef FEAT_AUTOCMD 5520 char_u *au_name = NULL; 5521 #endif 5522 5523 if (eap->cmdidx == CMD_lexpr || eap->cmdidx == CMD_lgetexpr 5524 || eap->cmdidx == CMD_laddexpr) 5525 { 5526 qi = ll_get_or_alloc_list(curwin); 5527 if (qi == NULL) 5528 return; 5529 } 5530 5531 #ifdef FEAT_AUTOCMD 5532 switch (eap->cmdidx) 5533 { 5534 case CMD_cexpr: au_name = (char_u *)"cexpr"; break; 5535 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break; 5536 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break; 5537 case CMD_lexpr: au_name = (char_u *)"lexpr"; break; 5538 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break; 5539 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break; 5540 default: break; 5541 } 5542 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 5543 curbuf->b_fname, TRUE, curbuf)) 5544 { 5545 # ifdef FEAT_EVAL 5546 if (aborting()) 5547 return; 5548 # endif 5549 } 5550 #endif 5551 5552 /* Evaluate the expression. When the result is a string or a list we can 5553 * use it to fill the errorlist. */ 5554 tv = eval_expr(eap->arg, NULL); 5555 if (tv != NULL) 5556 { 5557 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL) 5558 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)) 5559 { 5560 if (qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm, 5561 (eap->cmdidx != CMD_caddexpr 5562 && eap->cmdidx != CMD_laddexpr), 5563 (linenr_T)0, (linenr_T)0, *eap->cmdlinep, 5564 NULL) > 0) 5565 { 5566 #ifdef FEAT_AUTOCMD 5567 if (au_name != NULL) 5568 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 5569 curbuf->b_fname, TRUE, curbuf); 5570 #endif 5571 if (eap->cmdidx == CMD_cexpr || eap->cmdidx == CMD_lexpr) 5572 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 5573 } 5574 } 5575 else 5576 EMSG(_("E777: String or List expected")); 5577 free_tv(tv); 5578 } 5579 } 5580 #endif 5581 5582 /* 5583 * ":helpgrep {pattern}" 5584 */ 5585 void 5586 ex_helpgrep(exarg_T *eap) 5587 { 5588 regmatch_T regmatch; 5589 char_u *save_cpo; 5590 char_u *p; 5591 int fcount; 5592 char_u **fnames; 5593 FILE *fd; 5594 int fi; 5595 long lnum; 5596 #ifdef FEAT_MULTI_LANG 5597 char_u *lang; 5598 #endif 5599 qf_info_T *qi = &ql_info; 5600 qf_info_T *save_qi; 5601 int new_qi = FALSE; 5602 win_T *wp; 5603 #ifdef FEAT_AUTOCMD 5604 char_u *au_name = NULL; 5605 #endif 5606 5607 #ifdef FEAT_MULTI_LANG 5608 /* Check for a specified language */ 5609 lang = check_help_lang(eap->arg); 5610 #endif 5611 5612 #ifdef FEAT_AUTOCMD 5613 switch (eap->cmdidx) 5614 { 5615 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break; 5616 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break; 5617 default: break; 5618 } 5619 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 5620 curbuf->b_fname, TRUE, curbuf)) 5621 { 5622 # ifdef FEAT_EVAL 5623 if (aborting()) 5624 return; 5625 # endif 5626 } 5627 #endif 5628 5629 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 5630 save_cpo = p_cpo; 5631 p_cpo = empty_option; 5632 5633 if (eap->cmdidx == CMD_lhelpgrep) 5634 { 5635 /* If the current window is a help window, then use it */ 5636 if (bt_help(curwin->w_buffer)) 5637 wp = curwin; 5638 else 5639 /* Find an existing help window */ 5640 FOR_ALL_WINDOWS(wp) 5641 if (bt_help(wp->w_buffer)) 5642 break; 5643 5644 if (wp == NULL) /* Help window not found */ 5645 qi = NULL; 5646 else 5647 qi = wp->w_llist; 5648 5649 if (qi == NULL) 5650 { 5651 /* Allocate a new location list for help text matches */ 5652 if ((qi = ll_new_list()) == NULL) 5653 return; 5654 new_qi = TRUE; 5655 } 5656 } 5657 5658 /* Autocommands may change the list. Save it for later comparison */ 5659 save_qi = qi; 5660 5661 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING); 5662 regmatch.rm_ic = FALSE; 5663 if (regmatch.regprog != NULL) 5664 { 5665 #ifdef FEAT_MBYTE 5666 vimconv_T vc; 5667 5668 /* Help files are in utf-8 or latin1, convert lines when 'encoding' 5669 * differs. */ 5670 vc.vc_type = CONV_NONE; 5671 if (!enc_utf8) 5672 convert_setup(&vc, (char_u *)"utf-8", p_enc); 5673 #endif 5674 5675 /* create a new quickfix list */ 5676 qf_new_list(qi, *eap->cmdlinep); 5677 5678 /* Go through all directories in 'runtimepath' */ 5679 p = p_rtp; 5680 while (*p != NUL && !got_int) 5681 { 5682 copy_option_part(&p, NameBuff, MAXPATHL, ","); 5683 5684 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */ 5685 add_pathsep(NameBuff); 5686 STRCAT(NameBuff, "doc/*.\\(txt\\|??x\\)"); 5687 if (gen_expand_wildcards(1, &NameBuff, &fcount, 5688 &fnames, EW_FILE|EW_SILENT) == OK 5689 && fcount > 0) 5690 { 5691 for (fi = 0; fi < fcount && !got_int; ++fi) 5692 { 5693 #ifdef FEAT_MULTI_LANG 5694 /* Skip files for a different language. */ 5695 if (lang != NULL 5696 && STRNICMP(lang, fnames[fi] 5697 + STRLEN(fnames[fi]) - 3, 2) != 0 5698 && !(STRNICMP(lang, "en", 2) == 0 5699 && STRNICMP("txt", fnames[fi] 5700 + STRLEN(fnames[fi]) - 3, 3) == 0)) 5701 continue; 5702 #endif 5703 fd = mch_fopen((char *)fnames[fi], "r"); 5704 if (fd != NULL) 5705 { 5706 lnum = 1; 5707 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) 5708 { 5709 char_u *line = IObuff; 5710 #ifdef FEAT_MBYTE 5711 /* Convert a line if 'encoding' is not utf-8 and 5712 * the line contains a non-ASCII character. */ 5713 if (vc.vc_type != CONV_NONE 5714 && has_non_ascii(IObuff)) 5715 { 5716 line = string_convert(&vc, IObuff, NULL); 5717 if (line == NULL) 5718 line = IObuff; 5719 } 5720 #endif 5721 5722 if (vim_regexec(®match, line, (colnr_T)0)) 5723 { 5724 int l = (int)STRLEN(line); 5725 5726 /* remove trailing CR, LF, spaces, etc. */ 5727 while (l > 0 && line[l - 1] <= ' ') 5728 line[--l] = NUL; 5729 5730 if (qf_add_entry(qi, 5731 qi->qf_curlist, 5732 NULL, /* dir */ 5733 fnames[fi], 5734 0, 5735 line, 5736 lnum, 5737 (int)(regmatch.startp[0] - line) 5738 + 1, /* col */ 5739 FALSE, /* vis_col */ 5740 NULL, /* search pattern */ 5741 0, /* nr */ 5742 1, /* type */ 5743 TRUE /* valid */ 5744 ) == FAIL) 5745 { 5746 got_int = TRUE; 5747 #ifdef FEAT_MBYTE 5748 if (line != IObuff) 5749 vim_free(line); 5750 #endif 5751 break; 5752 } 5753 } 5754 #ifdef FEAT_MBYTE 5755 if (line != IObuff) 5756 vim_free(line); 5757 #endif 5758 ++lnum; 5759 line_breakcheck(); 5760 } 5761 fclose(fd); 5762 } 5763 } 5764 FreeWild(fcount, fnames); 5765 } 5766 } 5767 5768 vim_regfree(regmatch.regprog); 5769 #ifdef FEAT_MBYTE 5770 if (vc.vc_type != CONV_NONE) 5771 convert_setup(&vc, NULL, NULL); 5772 #endif 5773 5774 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 5775 qi->qf_lists[qi->qf_curlist].qf_ptr = 5776 qi->qf_lists[qi->qf_curlist].qf_start; 5777 qi->qf_lists[qi->qf_curlist].qf_index = 1; 5778 } 5779 5780 if (p_cpo == empty_option) 5781 p_cpo = save_cpo; 5782 else 5783 /* Darn, some plugin changed the value. */ 5784 free_string_option(save_cpo); 5785 5786 qf_update_buffer(qi, NULL); 5787 5788 #ifdef FEAT_AUTOCMD 5789 if (au_name != NULL) 5790 { 5791 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 5792 curbuf->b_fname, TRUE, curbuf); 5793 if (!new_qi && qi != save_qi && qf_find_buf(qi) == NULL) 5794 /* autocommands made "qi" invalid */ 5795 return; 5796 } 5797 #endif 5798 5799 /* Jump to first match. */ 5800 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 5801 qf_jump(qi, 0, 0, FALSE); 5802 else 5803 EMSG2(_(e_nomatch2), eap->arg); 5804 5805 if (eap->cmdidx == CMD_lhelpgrep) 5806 { 5807 /* If the help window is not opened or if it already points to the 5808 * correct location list, then free the new location list. */ 5809 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi) 5810 { 5811 if (new_qi) 5812 ll_free_all(&qi); 5813 } 5814 else if (curwin->w_llist == NULL) 5815 curwin->w_llist = qi; 5816 } 5817 } 5818 5819 #endif /* FEAT_QUICKFIX */ 5820