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