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 = FAIL; 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 = FAIL; 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, 4119 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL); 4120 if (browse_file == NULL) 4121 return; 4122 set_string_option_direct((char_u *)"ef", -1, browse_file, OPT_FREE, 0); 4123 vim_free(browse_file); 4124 } 4125 else 4126 #endif 4127 if (*eap->arg != NUL) 4128 set_string_option_direct((char_u *)"ef", -1, eap->arg, OPT_FREE, 0); 4129 4130 if (eap->cmdidx == CMD_lfile 4131 || eap->cmdidx == CMD_lgetfile 4132 || eap->cmdidx == CMD_laddfile) 4133 wp = curwin; 4134 4135 /* 4136 * This function is used by the :cfile, :cgetfile and :caddfile 4137 * commands. 4138 * :cfile always creates a new quickfix list and jumps to the 4139 * first error. 4140 * :cgetfile creates a new quickfix list but doesn't jump to the 4141 * first error. 4142 * :caddfile adds to an existing quickfix list. If there is no 4143 * quickfix list then a new list is created. 4144 */ 4145 res = qf_init(wp, p_ef, p_efm, (eap->cmdidx != CMD_caddfile 4146 && eap->cmdidx != CMD_laddfile), *eap->cmdlinep, enc); 4147 if (wp != NULL) 4148 qi = GET_LOC_LIST(wp); 4149 if (res >= 0 && qi != NULL) 4150 qf_list_changed(qi, qi->qf_curlist); 4151 if (qi != NULL) 4152 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; 4153 if (au_name != NULL) 4154 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, NULL, FALSE, curbuf); 4155 4156 /* An autocmd might have freed the quickfix/location list. Check whether it 4157 * is still valid. */ 4158 if (qi != NULL && !qflist_valid(wp, save_qfid)) 4159 return; 4160 if (res > 0 && (eap->cmdidx == CMD_cfile || eap->cmdidx == CMD_lfile)) 4161 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 4162 } 4163 4164 /* 4165 * Return the quickfix/location list number with the given identifier. 4166 * Returns -1 if list is not found. 4167 */ 4168 static int 4169 qf_id2nr(qf_info_T *qi, int_u qfid) 4170 { 4171 int qf_idx; 4172 4173 for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++) 4174 if (qi->qf_lists[qf_idx].qf_id == qfid) 4175 return qf_idx; 4176 return INVALID_QFIDX; 4177 } 4178 4179 /* 4180 * Return the vimgrep autocmd name. 4181 */ 4182 static char_u * 4183 vgr_get_auname(cmdidx_T cmdidx) 4184 { 4185 switch (cmdidx) 4186 { 4187 case CMD_vimgrep: return (char_u *)"vimgrep"; 4188 case CMD_lvimgrep: return (char_u *)"lvimgrep"; 4189 case CMD_vimgrepadd: return (char_u *)"vimgrepadd"; 4190 case CMD_lvimgrepadd: return (char_u *)"lvimgrepadd"; 4191 case CMD_grep: return (char_u *)"grep"; 4192 case CMD_lgrep: return (char_u *)"lgrep"; 4193 case CMD_grepadd: return (char_u *)"grepadd"; 4194 case CMD_lgrepadd: return (char_u *)"lgrepadd"; 4195 default: return NULL; 4196 } 4197 } 4198 4199 /* 4200 * Initialize the regmatch used by vimgrep for pattern "s". 4201 */ 4202 static void 4203 vgr_init_regmatch(regmmatch_T *regmatch, char_u *s) 4204 { 4205 /* Get the search pattern: either white-separated or enclosed in // */ 4206 regmatch->regprog = NULL; 4207 4208 if (s == NULL || *s == NUL) 4209 { 4210 /* Pattern is empty, use last search pattern. */ 4211 if (last_search_pat() == NULL) 4212 { 4213 EMSG(_(e_noprevre)); 4214 return; 4215 } 4216 regmatch->regprog = vim_regcomp(last_search_pat(), RE_MAGIC); 4217 } 4218 else 4219 regmatch->regprog = vim_regcomp(s, RE_MAGIC); 4220 4221 regmatch->rmm_ic = p_ic; 4222 regmatch->rmm_maxcol = 0; 4223 } 4224 4225 /* 4226 * Display a file name when vimgrep is running. 4227 */ 4228 static void 4229 vgr_display_fname(char_u *fname) 4230 { 4231 char_u *p; 4232 4233 msg_start(); 4234 p = msg_strtrunc(fname, TRUE); 4235 if (p == NULL) 4236 msg_outtrans(fname); 4237 else 4238 { 4239 msg_outtrans(p); 4240 vim_free(p); 4241 } 4242 msg_clr_eos(); 4243 msg_didout = FALSE; /* overwrite this message */ 4244 msg_nowait = TRUE; /* don't wait for this message */ 4245 msg_col = 0; 4246 out_flush(); 4247 } 4248 4249 /* 4250 * Load a dummy buffer to search for a pattern using vimgrep. 4251 */ 4252 static buf_T * 4253 vgr_load_dummy_buf( 4254 char_u *fname, 4255 char_u *dirname_start, 4256 char_u *dirname_now) 4257 { 4258 int save_mls; 4259 #if defined(FEAT_SYN_HL) 4260 char_u *save_ei = NULL; 4261 #endif 4262 buf_T *buf; 4263 4264 #if defined(FEAT_SYN_HL) 4265 /* Don't do Filetype autocommands to avoid loading syntax and 4266 * indent scripts, a great speed improvement. */ 4267 save_ei = au_event_disable(",Filetype"); 4268 #endif 4269 /* Don't use modelines here, it's useless. */ 4270 save_mls = p_mls; 4271 p_mls = 0; 4272 4273 /* Load file into a buffer, so that 'fileencoding' is detected, 4274 * autocommands applied, etc. */ 4275 buf = load_dummy_buffer(fname, dirname_start, dirname_now); 4276 4277 p_mls = save_mls; 4278 #if defined(FEAT_SYN_HL) 4279 au_event_restore(save_ei); 4280 #endif 4281 4282 return buf; 4283 } 4284 4285 /* 4286 * Check whether a quickfix/location list valid. Autocmds may remove or change 4287 * a quickfix list when vimgrep is running. If the list is not found, create a 4288 * new list. 4289 */ 4290 static int 4291 vgr_qflist_valid( 4292 win_T *wp, 4293 qf_info_T *qi, 4294 int_u qfid, 4295 char_u *title) 4296 { 4297 /* Verify that the quickfix/location list was not freed by an autocmd */ 4298 if (!qflist_valid(wp, qfid)) 4299 { 4300 if (wp != NULL) 4301 { 4302 /* An autocmd has freed the location list. */ 4303 EMSG(_(e_loc_list_changed)); 4304 return FALSE; 4305 } 4306 else 4307 { 4308 /* Quickfix list is not found, create a new one. */ 4309 qf_new_list(qi, title); 4310 return TRUE; 4311 } 4312 } 4313 4314 if (qi->qf_lists[qi->qf_curlist].qf_id != qfid) 4315 /* Autocommands changed the quickfix list. Find the one we were 4316 * using and restore it. */ 4317 qi->qf_curlist = qf_id2nr(qi, qfid); 4318 4319 return TRUE; 4320 } 4321 4322 /* 4323 * Search for a pattern in all the lines in a buffer and add the matching lines 4324 * to a quickfix list. 4325 */ 4326 static int 4327 vgr_match_buflines( 4328 qf_info_T *qi, 4329 char_u *fname, 4330 buf_T *buf, 4331 regmmatch_T *regmatch, 4332 long tomatch, 4333 int duplicate_name, 4334 int flags) 4335 { 4336 int found_match = FALSE; 4337 long lnum; 4338 colnr_T col; 4339 4340 for (lnum = 1; lnum <= buf->b_ml.ml_line_count && tomatch > 0; ++lnum) 4341 { 4342 col = 0; 4343 while (vim_regexec_multi(regmatch, curwin, buf, lnum, 4344 col, NULL, NULL) > 0) 4345 { 4346 /* Pass the buffer number so that it gets used even for a 4347 * dummy buffer, unless duplicate_name is set, then the 4348 * buffer will be wiped out below. */ 4349 if (qf_add_entry(qi, 4350 qi->qf_curlist, 4351 NULL, /* dir */ 4352 fname, 4353 duplicate_name ? 0 : buf->b_fnum, 4354 ml_get_buf(buf, 4355 regmatch->startpos[0].lnum + lnum, FALSE), 4356 regmatch->startpos[0].lnum + lnum, 4357 regmatch->startpos[0].col + 1, 4358 FALSE, /* vis_col */ 4359 NULL, /* search pattern */ 4360 0, /* nr */ 4361 0, /* type */ 4362 TRUE /* valid */ 4363 ) == FAIL) 4364 { 4365 got_int = TRUE; 4366 break; 4367 } 4368 found_match = TRUE; 4369 if (--tomatch == 0) 4370 break; 4371 if ((flags & VGR_GLOBAL) == 0 4372 || regmatch->endpos[0].lnum > 0) 4373 break; 4374 col = regmatch->endpos[0].col 4375 + (col == regmatch->endpos[0].col); 4376 if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, FALSE))) 4377 break; 4378 } 4379 line_breakcheck(); 4380 if (got_int) 4381 break; 4382 } 4383 4384 return found_match; 4385 } 4386 4387 /* 4388 * Jump to the first match and update the directory. 4389 */ 4390 static void 4391 vgr_jump_to_match( 4392 qf_info_T *qi, 4393 int forceit, 4394 int *redraw_for_dummy, 4395 buf_T *first_match_buf, 4396 char_u *target_dir) 4397 { 4398 buf_T *buf; 4399 4400 buf = curbuf; 4401 qf_jump(qi, 0, 0, forceit); 4402 if (buf != curbuf) 4403 /* If we jumped to another buffer redrawing will already be 4404 * taken care of. */ 4405 *redraw_for_dummy = FALSE; 4406 4407 /* Jump to the directory used after loading the buffer. */ 4408 if (curbuf == first_match_buf && target_dir != NULL) 4409 { 4410 exarg_T ea; 4411 4412 ea.arg = target_dir; 4413 ea.cmdidx = CMD_lcd; 4414 ex_cd(&ea); 4415 } 4416 } 4417 4418 /* 4419 * ":vimgrep {pattern} file(s)" 4420 * ":vimgrepadd {pattern} file(s)" 4421 * ":lvimgrep {pattern} file(s)" 4422 * ":lvimgrepadd {pattern} file(s)" 4423 */ 4424 void 4425 ex_vimgrep(exarg_T *eap) 4426 { 4427 regmmatch_T regmatch; 4428 int fcount; 4429 char_u **fnames; 4430 char_u *fname; 4431 char_u *title; 4432 char_u *s; 4433 char_u *p; 4434 int fi; 4435 qf_info_T *qi = &ql_info; 4436 int_u save_qfid; 4437 win_T *wp = NULL; 4438 buf_T *buf; 4439 int duplicate_name = FALSE; 4440 int using_dummy; 4441 int redraw_for_dummy = FALSE; 4442 int found_match; 4443 buf_T *first_match_buf = NULL; 4444 time_t seconds = 0; 4445 aco_save_T aco; 4446 int flags = 0; 4447 long tomatch; 4448 char_u *dirname_start = NULL; 4449 char_u *dirname_now = NULL; 4450 char_u *target_dir = NULL; 4451 char_u *au_name = NULL; 4452 4453 au_name = vgr_get_auname(eap->cmdidx); 4454 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 4455 curbuf->b_fname, TRUE, curbuf)) 4456 { 4457 #ifdef FEAT_EVAL 4458 if (aborting()) 4459 return; 4460 #endif 4461 } 4462 4463 if (eap->cmdidx == CMD_lgrep 4464 || eap->cmdidx == CMD_lvimgrep 4465 || eap->cmdidx == CMD_lgrepadd 4466 || eap->cmdidx == CMD_lvimgrepadd) 4467 { 4468 qi = ll_get_or_alloc_list(curwin); 4469 if (qi == NULL) 4470 return; 4471 wp = curwin; 4472 } 4473 4474 if (eap->addr_count > 0) 4475 tomatch = eap->line2; 4476 else 4477 tomatch = MAXLNUM; 4478 4479 /* Get the search pattern: either white-separated or enclosed in // */ 4480 regmatch.regprog = NULL; 4481 title = vim_strsave(*eap->cmdlinep); 4482 p = skip_vimgrep_pat(eap->arg, &s, &flags); 4483 if (p == NULL) 4484 { 4485 EMSG(_(e_invalpat)); 4486 goto theend; 4487 } 4488 4489 vgr_init_regmatch(®match, s); 4490 if (regmatch.regprog == NULL) 4491 goto theend; 4492 4493 p = skipwhite(p); 4494 if (*p == NUL) 4495 { 4496 EMSG(_("E683: File name missing or invalid pattern")); 4497 goto theend; 4498 } 4499 4500 if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd 4501 && eap->cmdidx != CMD_vimgrepadd 4502 && eap->cmdidx != CMD_lvimgrepadd) 4503 || qi->qf_curlist == qi->qf_listcount) 4504 /* make place for a new list */ 4505 qf_new_list(qi, title != NULL ? title : *eap->cmdlinep); 4506 4507 /* parse the list of arguments */ 4508 if (get_arglist_exp(p, &fcount, &fnames, TRUE) == FAIL) 4509 goto theend; 4510 if (fcount == 0) 4511 { 4512 EMSG(_(e_nomatch)); 4513 goto theend; 4514 } 4515 4516 dirname_start = alloc_id(MAXPATHL, aid_qf_dirname_start); 4517 dirname_now = alloc_id(MAXPATHL, aid_qf_dirname_now); 4518 if (dirname_start == NULL || dirname_now == NULL) 4519 { 4520 FreeWild(fcount, fnames); 4521 goto theend; 4522 } 4523 4524 /* Remember the current directory, because a BufRead autocommand that does 4525 * ":lcd %:p:h" changes the meaning of short path names. */ 4526 mch_dirname(dirname_start, MAXPATHL); 4527 4528 /* Remember the current quickfix list identifier, so that we can check for 4529 * autocommands changing the current quickfix list. */ 4530 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; 4531 4532 seconds = (time_t)0; 4533 for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi) 4534 { 4535 fname = shorten_fname1(fnames[fi]); 4536 if (time(NULL) > seconds) 4537 { 4538 /* Display the file name every second or so, show the user we are 4539 * working on it. */ 4540 seconds = time(NULL); 4541 vgr_display_fname(fname); 4542 } 4543 4544 buf = buflist_findname_exp(fnames[fi]); 4545 if (buf == NULL || buf->b_ml.ml_mfp == NULL) 4546 { 4547 /* Remember that a buffer with this name already exists. */ 4548 duplicate_name = (buf != NULL); 4549 using_dummy = TRUE; 4550 redraw_for_dummy = TRUE; 4551 4552 buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now); 4553 } 4554 else 4555 /* Use existing, loaded buffer. */ 4556 using_dummy = FALSE; 4557 4558 /* Check whether the quickfix list is still valid. When loading a 4559 * buffer above, autocommands might have changed the quickfix list. */ 4560 if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep)) 4561 { 4562 FreeWild(fcount, fnames); 4563 goto theend; 4564 } 4565 save_qfid = qi->qf_lists[qi->qf_curlist].qf_id; 4566 4567 if (buf == NULL) 4568 { 4569 if (!got_int) 4570 smsg((char_u *)_("Cannot open file \"%s\""), fname); 4571 } 4572 else 4573 { 4574 /* Try for a match in all lines of the buffer. 4575 * For ":1vimgrep" look for first match only. */ 4576 found_match = vgr_match_buflines(qi, fname, buf, ®match, 4577 tomatch, duplicate_name, flags); 4578 4579 if (using_dummy) 4580 { 4581 if (found_match && first_match_buf == NULL) 4582 first_match_buf = buf; 4583 if (duplicate_name) 4584 { 4585 /* Never keep a dummy buffer if there is another buffer 4586 * with the same name. */ 4587 wipe_dummy_buffer(buf, dirname_start); 4588 buf = NULL; 4589 } 4590 else if (!cmdmod.hide 4591 || buf->b_p_bh[0] == 'u' /* "unload" */ 4592 || buf->b_p_bh[0] == 'w' /* "wipe" */ 4593 || buf->b_p_bh[0] == 'd') /* "delete" */ 4594 { 4595 /* When no match was found we don't need to remember the 4596 * buffer, wipe it out. If there was a match and it 4597 * wasn't the first one or we won't jump there: only 4598 * unload the buffer. 4599 * Ignore 'hidden' here, because it may lead to having too 4600 * many swap files. */ 4601 if (!found_match) 4602 { 4603 wipe_dummy_buffer(buf, dirname_start); 4604 buf = NULL; 4605 } 4606 else if (buf != first_match_buf || (flags & VGR_NOJUMP)) 4607 { 4608 unload_dummy_buffer(buf, dirname_start); 4609 /* Keeping the buffer, remove the dummy flag. */ 4610 buf->b_flags &= ~BF_DUMMY; 4611 buf = NULL; 4612 } 4613 } 4614 4615 if (buf != NULL) 4616 { 4617 /* Keeping the buffer, remove the dummy flag. */ 4618 buf->b_flags &= ~BF_DUMMY; 4619 4620 /* If the buffer is still loaded we need to use the 4621 * directory we jumped to below. */ 4622 if (buf == first_match_buf 4623 && target_dir == NULL 4624 && STRCMP(dirname_start, dirname_now) != 0) 4625 target_dir = vim_strsave(dirname_now); 4626 4627 /* The buffer is still loaded, the Filetype autocommands 4628 * need to be done now, in that buffer. And the modelines 4629 * need to be done (again). But not the window-local 4630 * options! */ 4631 aucmd_prepbuf(&aco, buf); 4632 #if defined(FEAT_SYN_HL) 4633 apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, 4634 buf->b_fname, TRUE, buf); 4635 #endif 4636 do_modelines(OPT_NOWIN); 4637 aucmd_restbuf(&aco); 4638 } 4639 } 4640 } 4641 } 4642 4643 FreeWild(fcount, fnames); 4644 4645 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 4646 qi->qf_lists[qi->qf_curlist].qf_ptr = qi->qf_lists[qi->qf_curlist].qf_start; 4647 qi->qf_lists[qi->qf_curlist].qf_index = 1; 4648 qf_list_changed(qi, qi->qf_curlist); 4649 4650 qf_update_buffer(qi, NULL); 4651 4652 if (au_name != NULL) 4653 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 4654 curbuf->b_fname, TRUE, curbuf); 4655 /* 4656 * The QuickFixCmdPost autocmd may free the quickfix list. Check the list 4657 * is still valid. 4658 */ 4659 if (!qflist_valid(wp, save_qfid)) 4660 goto theend; 4661 4662 /* Jump to first match. */ 4663 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 4664 { 4665 if ((flags & VGR_NOJUMP) == 0) 4666 vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy, 4667 first_match_buf, target_dir); 4668 } 4669 else 4670 EMSG2(_(e_nomatch2), s); 4671 4672 /* If we loaded a dummy buffer into the current window, the autocommands 4673 * may have messed up things, need to redraw and recompute folds. */ 4674 if (redraw_for_dummy) 4675 { 4676 #ifdef FEAT_FOLDING 4677 foldUpdateAll(curwin); 4678 #else 4679 redraw_later(NOT_VALID); 4680 #endif 4681 } 4682 4683 theend: 4684 vim_free(title); 4685 vim_free(dirname_now); 4686 vim_free(dirname_start); 4687 vim_free(target_dir); 4688 vim_regfree(regmatch.regprog); 4689 } 4690 4691 /* 4692 * Restore current working directory to "dirname_start" if they differ, taking 4693 * into account whether it is set locally or globally. 4694 */ 4695 static void 4696 restore_start_dir(char_u *dirname_start) 4697 { 4698 char_u *dirname_now = alloc(MAXPATHL); 4699 4700 if (NULL != dirname_now) 4701 { 4702 mch_dirname(dirname_now, MAXPATHL); 4703 if (STRCMP(dirname_start, dirname_now) != 0) 4704 { 4705 /* If the directory has changed, change it back by building up an 4706 * appropriate ex command and executing it. */ 4707 exarg_T ea; 4708 4709 ea.arg = dirname_start; 4710 ea.cmdidx = (curwin->w_localdir == NULL) ? CMD_cd : CMD_lcd; 4711 ex_cd(&ea); 4712 } 4713 vim_free(dirname_now); 4714 } 4715 } 4716 4717 /* 4718 * Load file "fname" into a dummy buffer and return the buffer pointer, 4719 * placing the directory resulting from the buffer load into the 4720 * "resulting_dir" pointer. "resulting_dir" must be allocated by the caller 4721 * prior to calling this function. Restores directory to "dirname_start" prior 4722 * to returning, if autocmds or the 'autochdir' option have changed it. 4723 * 4724 * If creating the dummy buffer does not fail, must call unload_dummy_buffer() 4725 * or wipe_dummy_buffer() later! 4726 * 4727 * Returns NULL if it fails. 4728 */ 4729 static buf_T * 4730 load_dummy_buffer( 4731 char_u *fname, 4732 char_u *dirname_start, /* in: old directory */ 4733 char_u *resulting_dir) /* out: new directory */ 4734 { 4735 buf_T *newbuf; 4736 bufref_T newbufref; 4737 bufref_T newbuf_to_wipe; 4738 int failed = TRUE; 4739 aco_save_T aco; 4740 int readfile_result; 4741 4742 /* Allocate a buffer without putting it in the buffer list. */ 4743 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); 4744 if (newbuf == NULL) 4745 return NULL; 4746 set_bufref(&newbufref, newbuf); 4747 4748 /* Init the options. */ 4749 buf_copy_options(newbuf, BCO_ENTER | BCO_NOHELP); 4750 4751 /* need to open the memfile before putting the buffer in a window */ 4752 if (ml_open(newbuf) == OK) 4753 { 4754 /* Make sure this buffer isn't wiped out by auto commands. */ 4755 ++newbuf->b_locked; 4756 4757 /* set curwin/curbuf to buf and save a few things */ 4758 aucmd_prepbuf(&aco, newbuf); 4759 4760 /* Need to set the filename for autocommands. */ 4761 (void)setfname(curbuf, fname, NULL, FALSE); 4762 4763 /* Create swap file now to avoid the ATTENTION message. */ 4764 check_need_swap(TRUE); 4765 4766 /* Remove the "dummy" flag, otherwise autocommands may not 4767 * work. */ 4768 curbuf->b_flags &= ~BF_DUMMY; 4769 4770 newbuf_to_wipe.br_buf = NULL; 4771 readfile_result = readfile(fname, NULL, 4772 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, 4773 NULL, READ_NEW | READ_DUMMY); 4774 --newbuf->b_locked; 4775 if (readfile_result == OK 4776 && !got_int 4777 && !(curbuf->b_flags & BF_NEW)) 4778 { 4779 failed = FALSE; 4780 if (curbuf != newbuf) 4781 { 4782 /* Bloody autocommands changed the buffer! Can happen when 4783 * using netrw and editing a remote file. Use the current 4784 * buffer instead, delete the dummy one after restoring the 4785 * window stuff. */ 4786 set_bufref(&newbuf_to_wipe, newbuf); 4787 newbuf = curbuf; 4788 } 4789 } 4790 4791 /* restore curwin/curbuf and a few other things */ 4792 aucmd_restbuf(&aco); 4793 if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) 4794 wipe_buffer(newbuf_to_wipe.br_buf, FALSE); 4795 4796 /* Add back the "dummy" flag, otherwise buflist_findname_stat() won't 4797 * skip it. */ 4798 newbuf->b_flags |= BF_DUMMY; 4799 } 4800 4801 /* 4802 * When autocommands/'autochdir' option changed directory: go back. 4803 * Let the caller know what the resulting dir was first, in case it is 4804 * important. 4805 */ 4806 mch_dirname(resulting_dir, MAXPATHL); 4807 restore_start_dir(dirname_start); 4808 4809 if (!bufref_valid(&newbufref)) 4810 return NULL; 4811 if (failed) 4812 { 4813 wipe_dummy_buffer(newbuf, dirname_start); 4814 return NULL; 4815 } 4816 return newbuf; 4817 } 4818 4819 /* 4820 * Wipe out the dummy buffer that load_dummy_buffer() created. Restores 4821 * directory to "dirname_start" prior to returning, if autocmds or the 4822 * 'autochdir' option have changed it. 4823 */ 4824 static void 4825 wipe_dummy_buffer(buf_T *buf, char_u *dirname_start) 4826 { 4827 if (curbuf != buf) /* safety check */ 4828 { 4829 #if defined(FEAT_EVAL) 4830 cleanup_T cs; 4831 4832 /* Reset the error/interrupt/exception state here so that aborting() 4833 * returns FALSE when wiping out the buffer. Otherwise it doesn't 4834 * work when got_int is set. */ 4835 enter_cleanup(&cs); 4836 #endif 4837 4838 wipe_buffer(buf, FALSE); 4839 4840 #if defined(FEAT_EVAL) 4841 /* Restore the error/interrupt/exception state if not discarded by a 4842 * new aborting error, interrupt, or uncaught exception. */ 4843 leave_cleanup(&cs); 4844 #endif 4845 /* When autocommands/'autochdir' option changed directory: go back. */ 4846 restore_start_dir(dirname_start); 4847 } 4848 } 4849 4850 /* 4851 * Unload the dummy buffer that load_dummy_buffer() created. Restores 4852 * directory to "dirname_start" prior to returning, if autocmds or the 4853 * 'autochdir' option have changed it. 4854 */ 4855 static void 4856 unload_dummy_buffer(buf_T *buf, char_u *dirname_start) 4857 { 4858 if (curbuf != buf) /* safety check */ 4859 { 4860 close_buffer(NULL, buf, DOBUF_UNLOAD, FALSE); 4861 4862 /* When autocommands/'autochdir' option changed directory: go back. */ 4863 restore_start_dir(dirname_start); 4864 } 4865 } 4866 4867 #if defined(FEAT_EVAL) || defined(PROTO) 4868 /* 4869 * Add each quickfix error to list "list" as a dictionary. 4870 * If qf_idx is -1, use the current list. Otherwise, use the specified list. 4871 */ 4872 int 4873 get_errorlist(qf_info_T *qi_arg, win_T *wp, int qf_idx, list_T *list) 4874 { 4875 qf_info_T *qi = qi_arg; 4876 dict_T *dict; 4877 char_u buf[2]; 4878 qfline_T *qfp; 4879 int i; 4880 int bufnum; 4881 4882 if (qi == NULL) 4883 { 4884 qi = &ql_info; 4885 if (wp != NULL) 4886 { 4887 qi = GET_LOC_LIST(wp); 4888 if (qi == NULL) 4889 return FAIL; 4890 } 4891 } 4892 4893 if (qf_idx == INVALID_QFIDX) 4894 qf_idx = qi->qf_curlist; 4895 4896 if (qf_idx >= qi->qf_listcount 4897 || qi->qf_lists[qf_idx].qf_count == 0) 4898 return FAIL; 4899 4900 qfp = qi->qf_lists[qf_idx].qf_start; 4901 for (i = 1; !got_int && i <= qi->qf_lists[qf_idx].qf_count; ++i) 4902 { 4903 /* Handle entries with a non-existing buffer number. */ 4904 bufnum = qfp->qf_fnum; 4905 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 4906 bufnum = 0; 4907 4908 if ((dict = dict_alloc()) == NULL) 4909 return FAIL; 4910 if (list_append_dict(list, dict) == FAIL) 4911 return FAIL; 4912 4913 buf[0] = qfp->qf_type; 4914 buf[1] = NUL; 4915 if ( dict_add_nr_str(dict, "bufnr", (long)bufnum, NULL) == FAIL 4916 || dict_add_nr_str(dict, "lnum", (long)qfp->qf_lnum, NULL) == FAIL 4917 || dict_add_nr_str(dict, "col", (long)qfp->qf_col, NULL) == FAIL 4918 || dict_add_nr_str(dict, "vcol", (long)qfp->qf_viscol, NULL) == FAIL 4919 || dict_add_nr_str(dict, "nr", (long)qfp->qf_nr, NULL) == FAIL 4920 || dict_add_nr_str(dict, "pattern", 0L, 4921 qfp->qf_pattern == NULL ? (char_u *)"" : qfp->qf_pattern) == FAIL 4922 || dict_add_nr_str(dict, "text", 0L, 4923 qfp->qf_text == NULL ? (char_u *)"" : qfp->qf_text) == FAIL 4924 || dict_add_nr_str(dict, "type", 0L, buf) == FAIL 4925 || dict_add_nr_str(dict, "valid", (long)qfp->qf_valid, NULL) == FAIL) 4926 return FAIL; 4927 4928 qfp = qfp->qf_next; 4929 if (qfp == NULL) 4930 break; 4931 } 4932 return OK; 4933 } 4934 4935 /* 4936 * Flags used by getqflist()/getloclist() to determine which fields to return. 4937 */ 4938 enum { 4939 QF_GETLIST_NONE = 0x0, 4940 QF_GETLIST_TITLE = 0x1, 4941 QF_GETLIST_ITEMS = 0x2, 4942 QF_GETLIST_NR = 0x4, 4943 QF_GETLIST_WINID = 0x8, 4944 QF_GETLIST_CONTEXT = 0x10, 4945 QF_GETLIST_ID = 0x20, 4946 QF_GETLIST_IDX = 0x40, 4947 QF_GETLIST_SIZE = 0x80, 4948 QF_GETLIST_TICK = 0x100, 4949 QF_GETLIST_ALL = 0x1FF 4950 }; 4951 4952 /* 4953 * Parse text from 'di' and return the quickfix list items. 4954 * Existing quickfix lists are not modified. 4955 */ 4956 static int 4957 qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict) 4958 { 4959 int status = FAIL; 4960 qf_info_T *qi; 4961 char_u *errorformat = p_efm; 4962 dictitem_T *efm_di; 4963 list_T *l; 4964 4965 /* Only a List value is supported */ 4966 if (di->di_tv.v_type == VAR_LIST && di->di_tv.vval.v_list != NULL) 4967 { 4968 /* If errorformat is supplied then use it, otherwise use the 'efm' 4969 * option setting 4970 */ 4971 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL) 4972 { 4973 if (efm_di->di_tv.v_type != VAR_STRING || 4974 efm_di->di_tv.vval.v_string == NULL) 4975 return FAIL; 4976 errorformat = efm_di->di_tv.vval.v_string; 4977 } 4978 4979 l = list_alloc(); 4980 if (l == NULL) 4981 return FAIL; 4982 4983 qi = (qf_info_T *)alloc((unsigned)sizeof(qf_info_T)); 4984 if (qi != NULL) 4985 { 4986 vim_memset(qi, 0, (size_t)(sizeof(qf_info_T))); 4987 qi->qf_refcount++; 4988 4989 if (qf_init_ext(qi, 0, NULL, NULL, &di->di_tv, errorformat, 4990 TRUE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0) 4991 { 4992 (void)get_errorlist(qi, NULL, 0, l); 4993 qf_free(qi, 0); 4994 } 4995 free(qi); 4996 } 4997 dict_add_list(retdict, "items", l); 4998 status = OK; 4999 } 5000 5001 return status; 5002 } 5003 5004 /* 5005 * Return the quickfix/location list window identifier in the current tabpage. 5006 */ 5007 static int 5008 qf_winid(qf_info_T *qi) 5009 { 5010 win_T *win; 5011 5012 /* The quickfix window can be opened even if the quickfix list is not set 5013 * using ":copen". This is not true for location lists. */ 5014 if (qi == NULL) 5015 return 0; 5016 win = qf_find_win(qi); 5017 if (win != NULL) 5018 return win->w_id; 5019 return 0; 5020 } 5021 5022 /* 5023 * Convert the keys in 'what' to quickfix list property flags. 5024 */ 5025 static int 5026 qf_getprop_keys2flags(dict_T *what) 5027 { 5028 int flags = QF_GETLIST_NONE; 5029 5030 if (dict_find(what, (char_u *)"all", -1) != NULL) 5031 flags |= QF_GETLIST_ALL; 5032 5033 if (dict_find(what, (char_u *)"title", -1) != NULL) 5034 flags |= QF_GETLIST_TITLE; 5035 5036 if (dict_find(what, (char_u *)"nr", -1) != NULL) 5037 flags |= QF_GETLIST_NR; 5038 5039 if (dict_find(what, (char_u *)"winid", -1) != NULL) 5040 flags |= QF_GETLIST_WINID; 5041 5042 if (dict_find(what, (char_u *)"context", -1) != NULL) 5043 flags |= QF_GETLIST_CONTEXT; 5044 5045 if (dict_find(what, (char_u *)"id", -1) != NULL) 5046 flags |= QF_GETLIST_ID; 5047 5048 if (dict_find(what, (char_u *)"items", -1) != NULL) 5049 flags |= QF_GETLIST_ITEMS; 5050 5051 if (dict_find(what, (char_u *)"idx", -1) != NULL) 5052 flags |= QF_GETLIST_IDX; 5053 5054 if (dict_find(what, (char_u *)"size", -1) != NULL) 5055 flags |= QF_GETLIST_SIZE; 5056 5057 if (dict_find(what, (char_u *)"changedtick", -1) != NULL) 5058 flags |= QF_GETLIST_TICK; 5059 5060 return flags; 5061 } 5062 5063 /* 5064 * Return the quickfix list index based on 'nr' or 'id' in 'what'. 5065 * If 'nr' and 'id' are not present in 'what' then return the current 5066 * quickfix list index. 5067 * If 'nr' is zero then return the current quickfix list index. 5068 * If 'nr' is '$' then return the last quickfix list index. 5069 * If 'id' is present then return the index of the quickfix list with that id. 5070 * If 'id' is zero then return the quickfix list index specified by 'nr'. 5071 * Return -1, if quickfix list is not present or if the stack is empty. 5072 */ 5073 static int 5074 qf_getprop_qfidx(qf_info_T *qi, dict_T *what) 5075 { 5076 int qf_idx; 5077 dictitem_T *di; 5078 5079 qf_idx = qi->qf_curlist; /* default is the current list */ 5080 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL) 5081 { 5082 /* Use the specified quickfix/location list */ 5083 if (di->di_tv.v_type == VAR_NUMBER) 5084 { 5085 /* for zero use the current list */ 5086 if (di->di_tv.vval.v_number != 0) 5087 { 5088 qf_idx = di->di_tv.vval.v_number - 1; 5089 if (qf_idx < 0 || qf_idx >= qi->qf_listcount) 5090 qf_idx = INVALID_QFIDX; 5091 } 5092 } 5093 else if (di->di_tv.v_type == VAR_STRING 5094 && di->di_tv.vval.v_string != NULL 5095 && STRCMP(di->di_tv.vval.v_string, "$") == 0) 5096 /* Get the last quickfix list number */ 5097 qf_idx = qi->qf_listcount - 1; 5098 else 5099 qf_idx = INVALID_QFIDX; 5100 } 5101 5102 if ((di = dict_find(what, (char_u *)"id", -1)) != NULL) 5103 { 5104 /* Look for a list with the specified id */ 5105 if (di->di_tv.v_type == VAR_NUMBER) 5106 { 5107 /* 5108 * For zero, use the current list or the list specified by 'nr' 5109 */ 5110 if (di->di_tv.vval.v_number != 0) 5111 qf_idx = qf_id2nr(qi, di->di_tv.vval.v_number); 5112 } 5113 else 5114 qf_idx = INVALID_QFIDX; 5115 } 5116 5117 return qf_idx; 5118 } 5119 5120 /* 5121 * Return default values for quickfix list properties in retdict. 5122 */ 5123 static int 5124 qf_getprop_defaults(qf_info_T *qi, int flags, dict_T *retdict) 5125 { 5126 int status = OK; 5127 5128 if (flags & QF_GETLIST_TITLE) 5129 status = dict_add_nr_str(retdict, "title", 0L, (char_u *)""); 5130 if ((status == OK) && (flags & QF_GETLIST_ITEMS)) 5131 { 5132 list_T *l = list_alloc(); 5133 if (l != NULL) 5134 status = dict_add_list(retdict, "items", l); 5135 else 5136 status = FAIL; 5137 } 5138 if ((status == OK) && (flags & QF_GETLIST_NR)) 5139 status = dict_add_nr_str(retdict, "nr", 0L, NULL); 5140 if ((status == OK) && (flags & QF_GETLIST_WINID)) 5141 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL); 5142 if ((status == OK) && (flags & QF_GETLIST_CONTEXT)) 5143 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)""); 5144 if ((status == OK) && (flags & QF_GETLIST_ID)) 5145 status = dict_add_nr_str(retdict, "id", 0L, NULL); 5146 if ((status == OK) && (flags & QF_GETLIST_IDX)) 5147 status = dict_add_nr_str(retdict, "idx", 0L, NULL); 5148 if ((status == OK) && (flags & QF_GETLIST_SIZE)) 5149 status = dict_add_nr_str(retdict, "size", 0L, NULL); 5150 if ((status == OK) && (flags & QF_GETLIST_TICK)) 5151 status = dict_add_nr_str(retdict, "changedtick", 0L, NULL); 5152 5153 return status; 5154 } 5155 5156 /* 5157 * Return the quickfix list title as 'title' in retdict 5158 */ 5159 static int 5160 qf_getprop_title(qf_info_T *qi, int qf_idx, dict_T *retdict) 5161 { 5162 char_u *t; 5163 5164 t = qi->qf_lists[qf_idx].qf_title; 5165 if (t == NULL) 5166 t = (char_u *)""; 5167 return dict_add_nr_str(retdict, "title", 0L, t); 5168 } 5169 5170 /* 5171 * Return the quickfix list items/entries as 'items' in retdict 5172 */ 5173 static int 5174 qf_getprop_items(qf_info_T *qi, int qf_idx, dict_T *retdict) 5175 { 5176 int status = OK; 5177 list_T *l = list_alloc(); 5178 if (l != NULL) 5179 { 5180 (void)get_errorlist(qi, NULL, qf_idx, l); 5181 dict_add_list(retdict, "items", l); 5182 } 5183 else 5184 status = FAIL; 5185 5186 return status; 5187 } 5188 5189 /* 5190 * Return the quickfix list context (if any) as 'context' in retdict. 5191 */ 5192 static int 5193 qf_getprop_ctx(qf_info_T *qi, int qf_idx, dict_T *retdict) 5194 { 5195 int status; 5196 dictitem_T *di; 5197 5198 if (qi->qf_lists[qf_idx].qf_ctx != NULL) 5199 { 5200 di = dictitem_alloc((char_u *)"context"); 5201 if (di != NULL) 5202 { 5203 copy_tv(qi->qf_lists[qf_idx].qf_ctx, &di->di_tv); 5204 status = dict_add(retdict, di); 5205 if (status == FAIL) 5206 dictitem_free(di); 5207 } 5208 else 5209 status = FAIL; 5210 } 5211 else 5212 status = dict_add_nr_str(retdict, "context", 0L, (char_u *)""); 5213 5214 return status; 5215 } 5216 5217 /* 5218 * Return the quickfix list index as 'idx' in retdict 5219 */ 5220 static int 5221 qf_getprop_idx(qf_info_T *qi, int qf_idx, dict_T *retdict) 5222 { 5223 int idx = qi->qf_lists[qf_idx].qf_index; 5224 if (qi->qf_lists[qf_idx].qf_count == 0) 5225 /* For empty lists, qf_index is set to 1 */ 5226 idx = 0; 5227 return dict_add_nr_str(retdict, "idx", idx, NULL); 5228 } 5229 5230 /* 5231 * Return quickfix/location list details (title) as a 5232 * dictionary. 'what' contains the details to return. If 'list_idx' is -1, 5233 * then current list is used. Otherwise the specified list is used. 5234 */ 5235 int 5236 qf_get_properties(win_T *wp, dict_T *what, dict_T *retdict) 5237 { 5238 qf_info_T *qi = &ql_info; 5239 int status = OK; 5240 int qf_idx; 5241 dictitem_T *di; 5242 int flags = QF_GETLIST_NONE; 5243 5244 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL) 5245 return qf_get_list_from_lines(what, di, retdict); 5246 5247 if (wp != NULL) 5248 qi = GET_LOC_LIST(wp); 5249 5250 flags = qf_getprop_keys2flags(what); 5251 5252 if (qi != NULL && qi->qf_listcount != 0) 5253 qf_idx = qf_getprop_qfidx(qi, what); 5254 5255 /* List is not present or is empty */ 5256 if (qi == NULL || qi->qf_listcount == 0 || qf_idx == INVALID_QFIDX) 5257 return qf_getprop_defaults(qi, flags, retdict); 5258 5259 if (flags & QF_GETLIST_TITLE) 5260 status = qf_getprop_title(qi, qf_idx, retdict); 5261 if ((status == OK) && (flags & QF_GETLIST_NR)) 5262 status = dict_add_nr_str(retdict, "nr", qf_idx + 1, NULL); 5263 if ((status == OK) && (flags & QF_GETLIST_WINID)) 5264 status = dict_add_nr_str(retdict, "winid", qf_winid(qi), NULL); 5265 if ((status == OK) && (flags & QF_GETLIST_ITEMS)) 5266 status = qf_getprop_items(qi, qf_idx, retdict); 5267 if ((status == OK) && (flags & QF_GETLIST_CONTEXT)) 5268 status = qf_getprop_ctx(qi, qf_idx, retdict); 5269 if ((status == OK) && (flags & QF_GETLIST_ID)) 5270 status = dict_add_nr_str(retdict, "id", qi->qf_lists[qf_idx].qf_id, 5271 NULL); 5272 if ((status == OK) && (flags & QF_GETLIST_IDX)) 5273 status = qf_getprop_idx(qi, qf_idx, retdict); 5274 if ((status == OK) && (flags & QF_GETLIST_SIZE)) 5275 status = dict_add_nr_str(retdict, "size", 5276 qi->qf_lists[qf_idx].qf_count, NULL); 5277 if ((status == OK) && (flags & QF_GETLIST_TICK)) 5278 status = dict_add_nr_str(retdict, "changedtick", 5279 qi->qf_lists[qf_idx].qf_changedtick, NULL); 5280 5281 return status; 5282 } 5283 5284 /* 5285 * Add list of entries to quickfix/location list. Each list entry is 5286 * a dictionary with item information. 5287 */ 5288 static int 5289 qf_add_entries( 5290 qf_info_T *qi, 5291 int qf_idx, 5292 list_T *list, 5293 char_u *title, 5294 int action) 5295 { 5296 listitem_T *li; 5297 dict_T *d; 5298 char_u *filename, *pattern, *text, *type; 5299 int bufnum; 5300 long lnum; 5301 int col, nr; 5302 int vcol; 5303 qfline_T *old_last = NULL; 5304 int valid, status; 5305 int retval = OK; 5306 int did_bufnr_emsg = FALSE; 5307 5308 if (action == ' ' || qf_idx == qi->qf_listcount) 5309 { 5310 /* make place for a new list */ 5311 qf_new_list(qi, title); 5312 qf_idx = qi->qf_curlist; 5313 } 5314 else if (action == 'a' && qi->qf_lists[qf_idx].qf_count > 0) 5315 /* Adding to existing list, use last entry. */ 5316 old_last = qi->qf_lists[qf_idx].qf_last; 5317 else if (action == 'r') 5318 { 5319 qf_free_items(qi, qf_idx); 5320 qf_store_title(qi, qf_idx, title); 5321 } 5322 5323 for (li = list->lv_first; li != NULL; li = li->li_next) 5324 { 5325 if (li->li_tv.v_type != VAR_DICT) 5326 continue; /* Skip non-dict items */ 5327 5328 d = li->li_tv.vval.v_dict; 5329 if (d == NULL) 5330 continue; 5331 5332 filename = get_dict_string(d, (char_u *)"filename", TRUE); 5333 bufnum = (int)get_dict_number(d, (char_u *)"bufnr"); 5334 lnum = (int)get_dict_number(d, (char_u *)"lnum"); 5335 col = (int)get_dict_number(d, (char_u *)"col"); 5336 vcol = (int)get_dict_number(d, (char_u *)"vcol"); 5337 nr = (int)get_dict_number(d, (char_u *)"nr"); 5338 type = get_dict_string(d, (char_u *)"type", TRUE); 5339 pattern = get_dict_string(d, (char_u *)"pattern", TRUE); 5340 text = get_dict_string(d, (char_u *)"text", TRUE); 5341 if (text == NULL) 5342 text = vim_strsave((char_u *)""); 5343 5344 valid = TRUE; 5345 if ((filename == NULL && bufnum == 0) || (lnum == 0 && pattern == NULL)) 5346 valid = FALSE; 5347 5348 /* Mark entries with non-existing buffer number as not valid. Give the 5349 * error message only once. */ 5350 if (bufnum != 0 && (buflist_findnr(bufnum) == NULL)) 5351 { 5352 if (!did_bufnr_emsg) 5353 { 5354 did_bufnr_emsg = TRUE; 5355 EMSGN(_("E92: Buffer %ld not found"), bufnum); 5356 } 5357 valid = FALSE; 5358 bufnum = 0; 5359 } 5360 5361 /* If the 'valid' field is present it overrules the detected value. */ 5362 if ((dict_find(d, (char_u *)"valid", -1)) != NULL) 5363 valid = (int)get_dict_number(d, (char_u *)"valid"); 5364 5365 status = qf_add_entry(qi, 5366 qf_idx, 5367 NULL, /* dir */ 5368 filename, 5369 bufnum, 5370 text, 5371 lnum, 5372 col, 5373 vcol, /* vis_col */ 5374 pattern, /* search pattern */ 5375 nr, 5376 type == NULL ? NUL : *type, 5377 valid); 5378 5379 vim_free(filename); 5380 vim_free(pattern); 5381 vim_free(text); 5382 vim_free(type); 5383 5384 if (status == FAIL) 5385 { 5386 retval = FAIL; 5387 break; 5388 } 5389 } 5390 5391 if (qi->qf_lists[qf_idx].qf_index == 0) 5392 /* no valid entry */ 5393 qi->qf_lists[qf_idx].qf_nonevalid = TRUE; 5394 else 5395 qi->qf_lists[qf_idx].qf_nonevalid = FALSE; 5396 if (action != 'a') 5397 { 5398 qi->qf_lists[qf_idx].qf_ptr = 5399 qi->qf_lists[qf_idx].qf_start; 5400 if (qi->qf_lists[qf_idx].qf_count > 0) 5401 qi->qf_lists[qf_idx].qf_index = 1; 5402 } 5403 5404 /* Don't update the cursor in quickfix window when appending entries */ 5405 qf_update_buffer(qi, old_last); 5406 5407 return retval; 5408 } 5409 5410 /* 5411 * Get the quickfix list index from 'nr' or 'id' 5412 */ 5413 static int 5414 qf_setprop_get_qfidx( 5415 qf_info_T *qi, 5416 dict_T *what, 5417 int action, 5418 int *newlist) 5419 { 5420 dictitem_T *di; 5421 int qf_idx = qi->qf_curlist; /* default is the current list */ 5422 5423 if ((di = dict_find(what, (char_u *)"nr", -1)) != NULL) 5424 { 5425 /* Use the specified quickfix/location list */ 5426 if (di->di_tv.v_type == VAR_NUMBER) 5427 { 5428 /* for zero use the current list */ 5429 if (di->di_tv.vval.v_number != 0) 5430 qf_idx = di->di_tv.vval.v_number - 1; 5431 5432 if ((action == ' ' || action == 'a') && qf_idx == qi->qf_listcount) 5433 { 5434 /* 5435 * When creating a new list, accept qf_idx pointing to the next 5436 * non-available list and add the new list at the end of the 5437 * stack. 5438 */ 5439 *newlist = TRUE; 5440 qf_idx = qi->qf_listcount > 0 ? qi->qf_listcount - 1 : 0; 5441 } 5442 else if (qf_idx < 0 || qf_idx >= qi->qf_listcount) 5443 return INVALID_QFIDX; 5444 else if (action != ' ') 5445 *newlist = FALSE; /* use the specified list */ 5446 } 5447 else if (di->di_tv.v_type == VAR_STRING 5448 && di->di_tv.vval.v_string != NULL 5449 && STRCMP(di->di_tv.vval.v_string, "$") == 0) 5450 { 5451 if (qi->qf_listcount > 0) 5452 qf_idx = qi->qf_listcount - 1; 5453 else if (*newlist) 5454 qf_idx = 0; 5455 else 5456 return INVALID_QFIDX; 5457 } 5458 else 5459 return INVALID_QFIDX; 5460 } 5461 5462 if (!*newlist && (di = dict_find(what, (char_u *)"id", -1)) != NULL) 5463 { 5464 /* Use the quickfix/location list with the specified id */ 5465 if (di->di_tv.v_type != VAR_NUMBER) 5466 return INVALID_QFIDX; 5467 5468 return qf_id2nr(qi, di->di_tv.vval.v_number); 5469 } 5470 5471 return qf_idx; 5472 } 5473 5474 /* 5475 * Set the quickfix list title. 5476 */ 5477 static int 5478 qf_setprop_title(qf_info_T *qi, int qf_idx, dict_T *what, dictitem_T *di) 5479 { 5480 if (di->di_tv.v_type != VAR_STRING) 5481 return FAIL; 5482 5483 vim_free(qi->qf_lists[qf_idx].qf_title); 5484 qi->qf_lists[qf_idx].qf_title = 5485 get_dict_string(what, (char_u *)"title", TRUE); 5486 if (qf_idx == qi->qf_curlist) 5487 qf_update_win_titlevar(qi); 5488 5489 return OK; 5490 } 5491 5492 /* 5493 * Set quickfix list items/entries. 5494 */ 5495 static int 5496 qf_setprop_items(qf_info_T *qi, int qf_idx, dictitem_T *di, int action) 5497 { 5498 int retval = FAIL; 5499 char_u *title_save; 5500 5501 if (di->di_tv.v_type != VAR_LIST) 5502 return FAIL; 5503 5504 title_save = vim_strsave(qi->qf_lists[qf_idx].qf_title); 5505 retval = qf_add_entries(qi, qf_idx, di->di_tv.vval.v_list, 5506 title_save, action == ' ' ? 'a' : action); 5507 if (action == 'r') 5508 { 5509 /* 5510 * When replacing the quickfix list entries using 5511 * qf_add_entries(), the title is set with a ':' prefix. 5512 * Restore the title with the saved title. 5513 */ 5514 vim_free(qi->qf_lists[qf_idx].qf_title); 5515 qi->qf_lists[qf_idx].qf_title = vim_strsave(title_save); 5516 } 5517 vim_free(title_save); 5518 5519 return retval; 5520 } 5521 5522 /* 5523 * Set quickfix list items/entries from a list of lines. 5524 */ 5525 static int 5526 qf_setprop_items_from_lines( 5527 qf_info_T *qi, 5528 int qf_idx, 5529 dict_T *what, 5530 dictitem_T *di, 5531 int action) 5532 { 5533 char_u *errorformat = p_efm; 5534 dictitem_T *efm_di; 5535 int retval = FAIL; 5536 5537 /* Use the user supplied errorformat settings (if present) */ 5538 if ((efm_di = dict_find(what, (char_u *)"efm", -1)) != NULL) 5539 { 5540 if (efm_di->di_tv.v_type != VAR_STRING || 5541 efm_di->di_tv.vval.v_string == NULL) 5542 return FAIL; 5543 errorformat = efm_di->di_tv.vval.v_string; 5544 } 5545 5546 /* Only a List value is supported */ 5547 if (di->di_tv.v_type != VAR_LIST || di->di_tv.vval.v_list == NULL) 5548 return FAIL; 5549 5550 if (action == 'r') 5551 qf_free_items(qi, qf_idx); 5552 if (qf_init_ext(qi, qf_idx, NULL, NULL, &di->di_tv, errorformat, 5553 FALSE, (linenr_T)0, (linenr_T)0, NULL, NULL) > 0) 5554 retval = OK; 5555 5556 return retval; 5557 } 5558 5559 /* 5560 * Set quickfix list context. 5561 */ 5562 static int 5563 qf_setprop_context(qf_info_T *qi, int qf_idx, dictitem_T *di) 5564 { 5565 typval_T *ctx; 5566 5567 free_tv(qi->qf_lists[qf_idx].qf_ctx); 5568 ctx = alloc_tv(); 5569 if (ctx != NULL) 5570 copy_tv(&di->di_tv, ctx); 5571 qi->qf_lists[qf_idx].qf_ctx = ctx; 5572 5573 return OK; 5574 } 5575 5576 /* 5577 * Set quickfix/location list properties (title, items, context). 5578 * Also used to add items from parsing a list of lines. 5579 * Used by the setqflist() and setloclist() VimL functions. 5580 */ 5581 static int 5582 qf_set_properties(qf_info_T *qi, dict_T *what, int action, char_u *title) 5583 { 5584 dictitem_T *di; 5585 int retval = FAIL; 5586 int qf_idx; 5587 int newlist = FALSE; 5588 5589 if (action == ' ' || qi->qf_curlist == qi->qf_listcount) 5590 newlist = TRUE; 5591 5592 qf_idx = qf_setprop_get_qfidx(qi, what, action, &newlist); 5593 if (qf_idx == INVALID_QFIDX) /* List not found */ 5594 return FAIL; 5595 5596 if (newlist) 5597 { 5598 qi->qf_curlist = qf_idx; 5599 qf_new_list(qi, title); 5600 qf_idx = qi->qf_curlist; 5601 } 5602 5603 if ((di = dict_find(what, (char_u *)"title", -1)) != NULL) 5604 retval = qf_setprop_title(qi, qf_idx, what, di); 5605 if ((di = dict_find(what, (char_u *)"items", -1)) != NULL) 5606 retval = qf_setprop_items(qi, qf_idx, di, action); 5607 if ((di = dict_find(what, (char_u *)"lines", -1)) != NULL) 5608 retval = qf_setprop_items_from_lines(qi, qf_idx, what, di, action); 5609 if ((di = dict_find(what, (char_u *)"context", -1)) != NULL) 5610 retval = qf_setprop_context(qi, qf_idx, di); 5611 5612 if (retval == OK) 5613 qf_list_changed(qi, qf_idx); 5614 5615 return retval; 5616 } 5617 5618 /* 5619 * Find the non-location list window with the specified location list. 5620 */ 5621 static win_T * 5622 find_win_with_ll(qf_info_T *qi) 5623 { 5624 win_T *wp = NULL; 5625 5626 FOR_ALL_WINDOWS(wp) 5627 if ((wp->w_llist == qi) && !bt_quickfix(wp->w_buffer)) 5628 return wp; 5629 5630 return NULL; 5631 } 5632 5633 /* 5634 * Free the entire quickfix/location list stack. 5635 * If the quickfix/location list window is open, then clear it. 5636 */ 5637 static void 5638 qf_free_stack(win_T *wp, qf_info_T *qi) 5639 { 5640 win_T *qfwin = qf_find_win(qi); 5641 win_T *llwin = NULL; 5642 win_T *orig_wp = wp; 5643 5644 if (qfwin != NULL) 5645 { 5646 /* If the quickfix/location list window is open, then clear it */ 5647 if (qi->qf_curlist < qi->qf_listcount) 5648 qf_free(qi, qi->qf_curlist); 5649 qf_update_buffer(qi, NULL); 5650 } 5651 5652 if (wp != NULL && IS_LL_WINDOW(wp)) 5653 { 5654 /* If in the location list window, then use the non-location list 5655 * window with this location list (if present) 5656 */ 5657 llwin = find_win_with_ll(qi); 5658 if (llwin != NULL) 5659 wp = llwin; 5660 } 5661 5662 qf_free_all(wp); 5663 if (wp == NULL) 5664 { 5665 /* quickfix list */ 5666 qi->qf_curlist = 0; 5667 qi->qf_listcount = 0; 5668 } 5669 else if (IS_LL_WINDOW(orig_wp)) 5670 { 5671 /* If the location list window is open, then create a new empty 5672 * location list */ 5673 qf_info_T *new_ll = ll_new_list(); 5674 5675 /* first free the list reference in the location list window */ 5676 ll_free_all(&orig_wp->w_llist_ref); 5677 5678 orig_wp->w_llist_ref = new_ll; 5679 if (llwin != NULL) 5680 { 5681 llwin->w_llist = new_ll; 5682 new_ll->qf_refcount++; 5683 } 5684 } 5685 } 5686 5687 /* 5688 * Populate the quickfix list with the items supplied in the list 5689 * of dictionaries. "title" will be copied to w:quickfix_title. 5690 * "action" is 'a' for add, 'r' for replace. Otherwise create a new list. 5691 */ 5692 int 5693 set_errorlist( 5694 win_T *wp, 5695 list_T *list, 5696 int action, 5697 char_u *title, 5698 dict_T *what) 5699 { 5700 qf_info_T *qi = &ql_info; 5701 int retval = OK; 5702 5703 if (wp != NULL) 5704 { 5705 qi = ll_get_or_alloc_list(wp); 5706 if (qi == NULL) 5707 return FAIL; 5708 } 5709 5710 if (action == 'f') 5711 { 5712 /* Free the entire quickfix or location list stack */ 5713 qf_free_stack(wp, qi); 5714 } 5715 else if (what != NULL) 5716 retval = qf_set_properties(qi, what, action, title); 5717 else 5718 { 5719 retval = qf_add_entries(qi, qi->qf_curlist, list, title, action); 5720 if (retval == OK) 5721 qf_list_changed(qi, qi->qf_curlist); 5722 } 5723 5724 return retval; 5725 } 5726 5727 static int 5728 mark_quickfix_ctx(qf_info_T *qi, int copyID) 5729 { 5730 int i; 5731 int abort = FALSE; 5732 typval_T *ctx; 5733 5734 for (i = 0; i < LISTCOUNT && !abort; ++i) 5735 { 5736 ctx = qi->qf_lists[i].qf_ctx; 5737 if (ctx != NULL && ctx->v_type != VAR_NUMBER 5738 && ctx->v_type != VAR_STRING && ctx->v_type != VAR_FLOAT) 5739 abort = set_ref_in_item(ctx, copyID, NULL, NULL); 5740 } 5741 5742 return abort; 5743 } 5744 5745 /* 5746 * Mark the context of the quickfix list and the location lists (if present) as 5747 * "in use". So that garbage collection doesn't free the context. 5748 */ 5749 int 5750 set_ref_in_quickfix(int copyID) 5751 { 5752 int abort = FALSE; 5753 tabpage_T *tp; 5754 win_T *win; 5755 5756 abort = mark_quickfix_ctx(&ql_info, copyID); 5757 if (abort) 5758 return abort; 5759 5760 FOR_ALL_TAB_WINDOWS(tp, win) 5761 { 5762 if (win->w_llist != NULL) 5763 { 5764 abort = mark_quickfix_ctx(win->w_llist, copyID); 5765 if (abort) 5766 return abort; 5767 } 5768 if (IS_LL_WINDOW(win) && (win->w_llist_ref->qf_refcount == 1)) 5769 { 5770 /* In a location list window and none of the other windows is 5771 * referring to this location list. Mark the location list 5772 * context as still in use. 5773 */ 5774 abort = mark_quickfix_ctx(win->w_llist_ref, copyID); 5775 if (abort) 5776 return abort; 5777 } 5778 } 5779 5780 return abort; 5781 } 5782 #endif 5783 5784 /* 5785 * ":[range]cbuffer [bufnr]" command. 5786 * ":[range]caddbuffer [bufnr]" command. 5787 * ":[range]cgetbuffer [bufnr]" command. 5788 * ":[range]lbuffer [bufnr]" command. 5789 * ":[range]laddbuffer [bufnr]" command. 5790 * ":[range]lgetbuffer [bufnr]" command. 5791 */ 5792 void 5793 ex_cbuffer(exarg_T *eap) 5794 { 5795 buf_T *buf = NULL; 5796 qf_info_T *qi = &ql_info; 5797 char_u *au_name = NULL; 5798 int res; 5799 5800 switch (eap->cmdidx) 5801 { 5802 case CMD_cbuffer: au_name = (char_u *)"cbuffer"; break; 5803 case CMD_cgetbuffer: au_name = (char_u *)"cgetbuffer"; break; 5804 case CMD_caddbuffer: au_name = (char_u *)"caddbuffer"; break; 5805 case CMD_lbuffer: au_name = (char_u *)"lbuffer"; break; 5806 case CMD_lgetbuffer: au_name = (char_u *)"lgetbuffer"; break; 5807 case CMD_laddbuffer: au_name = (char_u *)"laddbuffer"; break; 5808 default: break; 5809 } 5810 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 5811 curbuf->b_fname, TRUE, curbuf)) 5812 { 5813 #ifdef FEAT_EVAL 5814 if (aborting()) 5815 return; 5816 #endif 5817 } 5818 5819 /* Must come after autocommands. */ 5820 if (eap->cmdidx == CMD_lbuffer 5821 || eap->cmdidx == CMD_lgetbuffer 5822 || eap->cmdidx == CMD_laddbuffer) 5823 { 5824 qi = ll_get_or_alloc_list(curwin); 5825 if (qi == NULL) 5826 return; 5827 } 5828 5829 if (*eap->arg == NUL) 5830 buf = curbuf; 5831 else if (*skipwhite(skipdigits(eap->arg)) == NUL) 5832 buf = buflist_findnr(atoi((char *)eap->arg)); 5833 if (buf == NULL) 5834 EMSG(_(e_invarg)); 5835 else if (buf->b_ml.ml_mfp == NULL) 5836 EMSG(_("E681: Buffer is not loaded")); 5837 else 5838 { 5839 if (eap->addr_count == 0) 5840 { 5841 eap->line1 = 1; 5842 eap->line2 = buf->b_ml.ml_line_count; 5843 } 5844 if (eap->line1 < 1 || eap->line1 > buf->b_ml.ml_line_count 5845 || eap->line2 < 1 || eap->line2 > buf->b_ml.ml_line_count) 5846 EMSG(_(e_invrange)); 5847 else 5848 { 5849 char_u *qf_title = *eap->cmdlinep; 5850 5851 if (buf->b_sfname) 5852 { 5853 vim_snprintf((char *)IObuff, IOSIZE, "%s (%s)", 5854 (char *)qf_title, (char *)buf->b_sfname); 5855 qf_title = IObuff; 5856 } 5857 5858 res = qf_init_ext(qi, qi->qf_curlist, NULL, buf, NULL, p_efm, 5859 (eap->cmdidx != CMD_caddbuffer 5860 && eap->cmdidx != CMD_laddbuffer), 5861 eap->line1, eap->line2, 5862 qf_title, NULL); 5863 if (res >= 0) 5864 qf_list_changed(qi, qi->qf_curlist); 5865 if (au_name != NULL) 5866 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 5867 curbuf->b_fname, TRUE, curbuf); 5868 if (res > 0 && (eap->cmdidx == CMD_cbuffer || 5869 eap->cmdidx == CMD_lbuffer)) 5870 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 5871 } 5872 } 5873 } 5874 5875 #if defined(FEAT_EVAL) || defined(PROTO) 5876 /* 5877 * ":cexpr {expr}", ":cgetexpr {expr}", ":caddexpr {expr}" command. 5878 * ":lexpr {expr}", ":lgetexpr {expr}", ":laddexpr {expr}" command. 5879 */ 5880 void 5881 ex_cexpr(exarg_T *eap) 5882 { 5883 typval_T *tv; 5884 qf_info_T *qi = &ql_info; 5885 char_u *au_name = NULL; 5886 int res; 5887 5888 switch (eap->cmdidx) 5889 { 5890 case CMD_cexpr: au_name = (char_u *)"cexpr"; break; 5891 case CMD_cgetexpr: au_name = (char_u *)"cgetexpr"; break; 5892 case CMD_caddexpr: au_name = (char_u *)"caddexpr"; break; 5893 case CMD_lexpr: au_name = (char_u *)"lexpr"; break; 5894 case CMD_lgetexpr: au_name = (char_u *)"lgetexpr"; break; 5895 case CMD_laddexpr: au_name = (char_u *)"laddexpr"; break; 5896 default: break; 5897 } 5898 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 5899 curbuf->b_fname, TRUE, curbuf)) 5900 { 5901 #ifdef FEAT_EVAL 5902 if (aborting()) 5903 return; 5904 #endif 5905 } 5906 5907 if (eap->cmdidx == CMD_lexpr 5908 || eap->cmdidx == CMD_lgetexpr 5909 || eap->cmdidx == CMD_laddexpr) 5910 { 5911 qi = ll_get_or_alloc_list(curwin); 5912 if (qi == NULL) 5913 return; 5914 } 5915 5916 /* Evaluate the expression. When the result is a string or a list we can 5917 * use it to fill the errorlist. */ 5918 tv = eval_expr(eap->arg, NULL); 5919 if (tv != NULL) 5920 { 5921 if ((tv->v_type == VAR_STRING && tv->vval.v_string != NULL) 5922 || (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)) 5923 { 5924 res = qf_init_ext(qi, qi->qf_curlist, NULL, NULL, tv, p_efm, 5925 (eap->cmdidx != CMD_caddexpr 5926 && eap->cmdidx != CMD_laddexpr), 5927 (linenr_T)0, (linenr_T)0, *eap->cmdlinep, 5928 NULL); 5929 if (res >= 0) 5930 qf_list_changed(qi, qi->qf_curlist); 5931 if (au_name != NULL) 5932 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 5933 curbuf->b_fname, TRUE, curbuf); 5934 if (res > 0 && (eap->cmdidx == CMD_cexpr || 5935 eap->cmdidx == CMD_lexpr)) 5936 qf_jump(qi, 0, 0, eap->forceit); /* display first error */ 5937 } 5938 else 5939 EMSG(_("E777: String or List expected")); 5940 free_tv(tv); 5941 } 5942 } 5943 #endif 5944 5945 /* 5946 * Get the location list for ":lhelpgrep" 5947 */ 5948 static qf_info_T * 5949 hgr_get_ll(int *new_ll) 5950 { 5951 win_T *wp; 5952 qf_info_T *qi; 5953 5954 /* If the current window is a help window, then use it */ 5955 if (bt_help(curwin->w_buffer)) 5956 wp = curwin; 5957 else 5958 /* Find an existing help window */ 5959 FOR_ALL_WINDOWS(wp) 5960 if (bt_help(wp->w_buffer)) 5961 break; 5962 5963 if (wp == NULL) /* Help window not found */ 5964 qi = NULL; 5965 else 5966 qi = wp->w_llist; 5967 5968 if (qi == NULL) 5969 { 5970 /* Allocate a new location list for help text matches */ 5971 if ((qi = ll_new_list()) == NULL) 5972 return NULL; 5973 *new_ll = TRUE; 5974 } 5975 5976 return qi; 5977 } 5978 5979 /* 5980 * Search for a pattern in a help file. 5981 */ 5982 static void 5983 hgr_search_file( 5984 qf_info_T *qi, 5985 char_u *fname, 5986 #ifdef FEAT_MBYTE 5987 vimconv_T *p_vc, 5988 #endif 5989 regmatch_T *p_regmatch) 5990 { 5991 FILE *fd; 5992 long lnum; 5993 5994 fd = mch_fopen((char *)fname, "r"); 5995 if (fd == NULL) 5996 return; 5997 5998 lnum = 1; 5999 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) 6000 { 6001 char_u *line = IObuff; 6002 #ifdef FEAT_MBYTE 6003 /* Convert a line if 'encoding' is not utf-8 and 6004 * the line contains a non-ASCII character. */ 6005 if (p_vc->vc_type != CONV_NONE 6006 && has_non_ascii(IObuff)) 6007 { 6008 line = string_convert(p_vc, IObuff, NULL); 6009 if (line == NULL) 6010 line = IObuff; 6011 } 6012 #endif 6013 6014 if (vim_regexec(p_regmatch, line, (colnr_T)0)) 6015 { 6016 int l = (int)STRLEN(line); 6017 6018 /* remove trailing CR, LF, spaces, etc. */ 6019 while (l > 0 && line[l - 1] <= ' ') 6020 line[--l] = NUL; 6021 6022 if (qf_add_entry(qi, 6023 qi->qf_curlist, 6024 NULL, /* dir */ 6025 fname, 6026 0, 6027 line, 6028 lnum, 6029 (int)(p_regmatch->startp[0] - line) 6030 + 1, /* col */ 6031 FALSE, /* vis_col */ 6032 NULL, /* search pattern */ 6033 0, /* nr */ 6034 1, /* type */ 6035 TRUE /* valid */ 6036 ) == FAIL) 6037 { 6038 got_int = TRUE; 6039 #ifdef FEAT_MBYTE 6040 if (line != IObuff) 6041 vim_free(line); 6042 #endif 6043 break; 6044 } 6045 } 6046 #ifdef FEAT_MBYTE 6047 if (line != IObuff) 6048 vim_free(line); 6049 #endif 6050 ++lnum; 6051 line_breakcheck(); 6052 } 6053 fclose(fd); 6054 } 6055 6056 /* 6057 * Search for a pattern in all the help files in the doc directory under 6058 * the given directory. 6059 */ 6060 static void 6061 hgr_search_files_in_dir( 6062 qf_info_T *qi, 6063 char_u *dirname, 6064 regmatch_T *p_regmatch 6065 #ifdef FEAT_MBYTE 6066 , vimconv_T *p_vc 6067 #endif 6068 #ifdef FEAT_MULTI_LANG 6069 , char_u *lang 6070 #endif 6071 ) 6072 { 6073 int fcount; 6074 char_u **fnames; 6075 int fi; 6076 6077 /* Find all "*.txt" and "*.??x" files in the "doc" directory. */ 6078 add_pathsep(dirname); 6079 STRCAT(dirname, "doc/*.\\(txt\\|??x\\)"); 6080 if (gen_expand_wildcards(1, &dirname, &fcount, 6081 &fnames, EW_FILE|EW_SILENT) == OK 6082 && fcount > 0) 6083 { 6084 for (fi = 0; fi < fcount && !got_int; ++fi) 6085 { 6086 #ifdef FEAT_MULTI_LANG 6087 /* Skip files for a different language. */ 6088 if (lang != NULL 6089 && STRNICMP(lang, fnames[fi] 6090 + STRLEN(fnames[fi]) - 3, 2) != 0 6091 && !(STRNICMP(lang, "en", 2) == 0 6092 && STRNICMP("txt", fnames[fi] 6093 + STRLEN(fnames[fi]) - 3, 3) == 0)) 6094 continue; 6095 #endif 6096 6097 hgr_search_file(qi, fnames[fi], 6098 #ifdef FEAT_MBYTE 6099 p_vc, 6100 #endif 6101 p_regmatch); 6102 } 6103 FreeWild(fcount, fnames); 6104 } 6105 } 6106 6107 /* 6108 * Search for a pattern in all the help files in the 'runtimepath'. 6109 */ 6110 static void 6111 hgr_search_in_rtp(qf_info_T *qi, regmatch_T *p_regmatch, char_u *arg) 6112 { 6113 char_u *p; 6114 #ifdef FEAT_MULTI_LANG 6115 char_u *lang; 6116 #endif 6117 6118 #ifdef FEAT_MBYTE 6119 vimconv_T vc; 6120 6121 /* Help files are in utf-8 or latin1, convert lines when 'encoding' 6122 * differs. */ 6123 vc.vc_type = CONV_NONE; 6124 if (!enc_utf8) 6125 convert_setup(&vc, (char_u *)"utf-8", p_enc); 6126 #endif 6127 6128 #ifdef FEAT_MULTI_LANG 6129 /* Check for a specified language */ 6130 lang = check_help_lang(arg); 6131 #endif 6132 6133 /* Go through all directories in 'runtimepath' */ 6134 p = p_rtp; 6135 while (*p != NUL && !got_int) 6136 { 6137 copy_option_part(&p, NameBuff, MAXPATHL, ","); 6138 6139 hgr_search_files_in_dir(qi, NameBuff, p_regmatch 6140 #ifdef FEAT_MBYTE 6141 , &vc 6142 #endif 6143 #ifdef FEAT_MULTI_LANG 6144 , lang 6145 #endif 6146 ); 6147 } 6148 6149 #ifdef FEAT_MBYTE 6150 if (vc.vc_type != CONV_NONE) 6151 convert_setup(&vc, NULL, NULL); 6152 #endif 6153 } 6154 6155 /* 6156 * ":helpgrep {pattern}" 6157 */ 6158 void 6159 ex_helpgrep(exarg_T *eap) 6160 { 6161 regmatch_T regmatch; 6162 char_u *save_cpo; 6163 qf_info_T *qi = &ql_info; 6164 int new_qi = FALSE; 6165 char_u *au_name = NULL; 6166 6167 switch (eap->cmdidx) 6168 { 6169 case CMD_helpgrep: au_name = (char_u *)"helpgrep"; break; 6170 case CMD_lhelpgrep: au_name = (char_u *)"lhelpgrep"; break; 6171 default: break; 6172 } 6173 if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, 6174 curbuf->b_fname, TRUE, curbuf)) 6175 { 6176 #ifdef FEAT_EVAL 6177 if (aborting()) 6178 return; 6179 #endif 6180 } 6181 6182 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */ 6183 save_cpo = p_cpo; 6184 p_cpo = empty_option; 6185 6186 if (eap->cmdidx == CMD_lhelpgrep) 6187 { 6188 qi = hgr_get_ll(&new_qi); 6189 if (qi == NULL) 6190 return; 6191 } 6192 6193 regmatch.regprog = vim_regcomp(eap->arg, RE_MAGIC + RE_STRING); 6194 regmatch.rm_ic = FALSE; 6195 if (regmatch.regprog != NULL) 6196 { 6197 /* create a new quickfix list */ 6198 qf_new_list(qi, *eap->cmdlinep); 6199 6200 hgr_search_in_rtp(qi, ®match, eap->arg); 6201 6202 vim_regfree(regmatch.regprog); 6203 6204 qi->qf_lists[qi->qf_curlist].qf_nonevalid = FALSE; 6205 qi->qf_lists[qi->qf_curlist].qf_ptr = 6206 qi->qf_lists[qi->qf_curlist].qf_start; 6207 qi->qf_lists[qi->qf_curlist].qf_index = 1; 6208 } 6209 6210 if (p_cpo == empty_option) 6211 p_cpo = save_cpo; 6212 else 6213 /* Darn, some plugin changed the value. */ 6214 free_string_option(save_cpo); 6215 6216 qf_list_changed(qi, qi->qf_curlist); 6217 qf_update_buffer(qi, NULL); 6218 6219 if (au_name != NULL) 6220 { 6221 apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, 6222 curbuf->b_fname, TRUE, curbuf); 6223 if (!new_qi && qi != &ql_info && qf_find_buf(qi) == NULL) 6224 /* autocommands made "qi" invalid */ 6225 return; 6226 } 6227 6228 /* Jump to first match. */ 6229 if (qi->qf_lists[qi->qf_curlist].qf_count > 0) 6230 qf_jump(qi, 0, 0, FALSE); 6231 else 6232 EMSG2(_(e_nomatch2), eap->arg); 6233 6234 if (eap->cmdidx == CMD_lhelpgrep) 6235 { 6236 /* If the help window is not opened or if it already points to the 6237 * correct location list, then free the new location list. */ 6238 if (!bt_help(curwin->w_buffer) || curwin->w_llist == qi) 6239 { 6240 if (new_qi) 6241 ll_free_all(&qi); 6242 } 6243 else if (curwin->w_llist == NULL) 6244 curwin->w_llist = qi; 6245 } 6246 } 6247 6248 #endif /* FEAT_QUICKFIX */ 6249