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