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