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