1 /* vi:set ts=8 sts=4 sw=4: 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 * ex_cmds.c: some functions for command line commands 12 */ 13 14 #include "vim.h" 15 #include "version.h" 16 17 #ifdef FEAT_FLOAT 18 # include <float.h> 19 #endif 20 21 static int linelen(int *has_tab); 22 static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out); 23 #ifdef FEAT_VIMINFO 24 static char_u *viminfo_filename(char_u *); 25 static void do_viminfo(FILE *fp_in, FILE *fp_out, int flags); 26 static int viminfo_encoding(vir_T *virp); 27 static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing); 28 #endif 29 30 static int check_readonly(int *forceit, buf_T *buf); 31 #ifdef FEAT_AUTOCMD 32 static void delbuf_msg(char_u *name); 33 #endif 34 static int 35 #ifdef __BORLANDC__ 36 _RTLENTRYF 37 #endif 38 help_compare(const void *s1, const void *s2); 39 static void prepare_help_buffer(void); 40 41 /* 42 * ":ascii" and "ga". 43 */ 44 void 45 do_ascii(exarg_T *eap UNUSED) 46 { 47 int c; 48 int cval; 49 char buf1[20]; 50 char buf2[20]; 51 char_u buf3[7]; 52 #ifdef FEAT_MBYTE 53 int cc[MAX_MCO]; 54 int ci = 0; 55 int len; 56 57 if (enc_utf8) 58 c = utfc_ptr2char(ml_get_cursor(), cc); 59 else 60 #endif 61 c = gchar_cursor(); 62 if (c == NUL) 63 { 64 MSG("NUL"); 65 return; 66 } 67 68 #ifdef FEAT_MBYTE 69 IObuff[0] = NUL; 70 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) 71 #endif 72 { 73 if (c == NL) /* NUL is stored as NL */ 74 c = NUL; 75 if (c == CAR && get_fileformat(curbuf) == EOL_MAC) 76 cval = NL; /* NL is stored as CR */ 77 else 78 cval = c; 79 if (vim_isprintc_strict(c) && (c < ' ' 80 #ifndef EBCDIC 81 || c > '~' 82 #endif 83 )) 84 { 85 transchar_nonprint(buf3, c); 86 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); 87 } 88 else 89 buf1[0] = NUL; 90 #ifndef EBCDIC 91 if (c >= 0x80) 92 vim_snprintf(buf2, sizeof(buf2), " <M-%s>", 93 (char *)transchar(c & 0x7f)); 94 else 95 #endif 96 buf2[0] = NUL; 97 vim_snprintf((char *)IObuff, IOSIZE, 98 _("<%s>%s%s %d, Hex %02x, Octal %03o"), 99 transchar(c), buf1, buf2, cval, cval, cval); 100 #ifdef FEAT_MBYTE 101 if (enc_utf8) 102 c = cc[ci++]; 103 else 104 c = 0; 105 #endif 106 } 107 108 #ifdef FEAT_MBYTE 109 /* Repeat for combining characters. */ 110 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) 111 { 112 len = (int)STRLEN(IObuff); 113 /* This assumes every multi-byte char is printable... */ 114 if (len > 0) 115 IObuff[len++] = ' '; 116 IObuff[len++] = '<'; 117 if (enc_utf8 && utf_iscomposing(c) 118 # ifdef USE_GUI 119 && !gui.in_use 120 # endif 121 ) 122 IObuff[len++] = ' '; /* draw composing char on top of a space */ 123 len += (*mb_char2bytes)(c, IObuff + len); 124 vim_snprintf((char *)IObuff + len, IOSIZE - len, 125 c < 0x10000 ? _("> %d, Hex %04x, Octal %o") 126 : _("> %d, Hex %08x, Octal %o"), c, c, c); 127 if (ci == MAX_MCO) 128 break; 129 if (enc_utf8) 130 c = cc[ci++]; 131 else 132 c = 0; 133 } 134 #endif 135 136 msg(IObuff); 137 } 138 139 /* 140 * ":left", ":center" and ":right": align text. 141 */ 142 void 143 ex_align(exarg_T *eap) 144 { 145 pos_T save_curpos; 146 int len; 147 int indent = 0; 148 int new_indent; 149 int has_tab; 150 int width; 151 152 #ifdef FEAT_RIGHTLEFT 153 if (curwin->w_p_rl) 154 { 155 /* switch left and right aligning */ 156 if (eap->cmdidx == CMD_right) 157 eap->cmdidx = CMD_left; 158 else if (eap->cmdidx == CMD_left) 159 eap->cmdidx = CMD_right; 160 } 161 #endif 162 163 width = atoi((char *)eap->arg); 164 save_curpos = curwin->w_cursor; 165 if (eap->cmdidx == CMD_left) /* width is used for new indent */ 166 { 167 if (width >= 0) 168 indent = width; 169 } 170 else 171 { 172 /* 173 * if 'textwidth' set, use it 174 * else if 'wrapmargin' set, use it 175 * if invalid value, use 80 176 */ 177 if (width <= 0) 178 width = curbuf->b_p_tw; 179 if (width == 0 && curbuf->b_p_wm > 0) 180 width = W_WIDTH(curwin) - curbuf->b_p_wm; 181 if (width <= 0) 182 width = 80; 183 } 184 185 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) 186 return; 187 188 for (curwin->w_cursor.lnum = eap->line1; 189 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) 190 { 191 if (eap->cmdidx == CMD_left) /* left align */ 192 new_indent = indent; 193 else 194 { 195 has_tab = FALSE; /* avoid uninit warnings */ 196 len = linelen(eap->cmdidx == CMD_right ? &has_tab 197 : NULL) - get_indent(); 198 199 if (len <= 0) /* skip blank lines */ 200 continue; 201 202 if (eap->cmdidx == CMD_center) 203 new_indent = (width - len) / 2; 204 else 205 { 206 new_indent = width - len; /* right align */ 207 208 /* 209 * Make sure that embedded TABs don't make the text go too far 210 * to the right. 211 */ 212 if (has_tab) 213 while (new_indent > 0) 214 { 215 (void)set_indent(new_indent, 0); 216 if (linelen(NULL) <= width) 217 { 218 /* 219 * Now try to move the line as much as possible to 220 * the right. Stop when it moves too far. 221 */ 222 do 223 (void)set_indent(++new_indent, 0); 224 while (linelen(NULL) <= width); 225 --new_indent; 226 break; 227 } 228 --new_indent; 229 } 230 } 231 } 232 if (new_indent < 0) 233 new_indent = 0; 234 (void)set_indent(new_indent, 0); /* set indent */ 235 } 236 changed_lines(eap->line1, 0, eap->line2 + 1, 0L); 237 curwin->w_cursor = save_curpos; 238 beginline(BL_WHITE | BL_FIX); 239 } 240 241 /* 242 * Get the length of the current line, excluding trailing white space. 243 */ 244 static int 245 linelen(int *has_tab) 246 { 247 char_u *line; 248 char_u *first; 249 char_u *last; 250 int save; 251 int len; 252 253 /* find the first non-blank character */ 254 line = ml_get_curline(); 255 first = skipwhite(line); 256 257 /* find the character after the last non-blank character */ 258 for (last = first + STRLEN(first); 259 last > first && vim_iswhite(last[-1]); --last) 260 ; 261 save = *last; 262 *last = NUL; 263 len = linetabsize(line); /* get line length */ 264 if (has_tab != NULL) /* check for embedded TAB */ 265 *has_tab = (vim_strrchr(first, TAB) != NULL); 266 *last = save; 267 268 return len; 269 } 270 271 /* Buffer for two lines used during sorting. They are allocated to 272 * contain the longest line being sorted. */ 273 static char_u *sortbuf1; 274 static char_u *sortbuf2; 275 276 static int sort_ic; /* ignore case */ 277 static int sort_nr; /* sort on number */ 278 static int sort_rx; /* sort on regex instead of skipping it */ 279 #ifdef FEAT_FLOAT 280 static int sort_flt; /* sort on floating number */ 281 #endif 282 283 static int sort_abort; /* flag to indicate if sorting has been interrupted */ 284 285 /* Struct to store info to be sorted. */ 286 typedef struct 287 { 288 linenr_T lnum; /* line number */ 289 union { 290 struct 291 { 292 long start_col_nr; /* starting column number */ 293 long end_col_nr; /* ending column number */ 294 } line; 295 long value; /* value if sorting by integer */ 296 #ifdef FEAT_FLOAT 297 float_T value_flt; /* value if sorting by float */ 298 #endif 299 } st_u; 300 } sorti_T; 301 302 static int 303 #ifdef __BORLANDC__ 304 _RTLENTRYF 305 #endif 306 sort_compare(const void *s1, const void *s2); 307 308 static int 309 #ifdef __BORLANDC__ 310 _RTLENTRYF 311 #endif 312 sort_compare(const void *s1, const void *s2) 313 { 314 sorti_T l1 = *(sorti_T *)s1; 315 sorti_T l2 = *(sorti_T *)s2; 316 int result = 0; 317 318 /* If the user interrupts, there's no way to stop qsort() immediately, but 319 * if we return 0 every time, qsort will assume it's done sorting and 320 * exit. */ 321 if (sort_abort) 322 return 0; 323 fast_breakcheck(); 324 if (got_int) 325 sort_abort = TRUE; 326 327 /* When sorting numbers "start_col_nr" is the number, not the column 328 * number. */ 329 if (sort_nr) 330 result = l1.st_u.value == l2.st_u.value ? 0 331 : l1.st_u.value > l2.st_u.value ? 1 : -1; 332 #ifdef FEAT_FLOAT 333 else if (sort_flt) 334 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0 335 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1; 336 #endif 337 else 338 { 339 /* We need to copy one line into "sortbuf1", because there is no 340 * guarantee that the first pointer becomes invalid when obtaining the 341 * second one. */ 342 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr, 343 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1); 344 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0; 345 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr, 346 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1); 347 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0; 348 349 result = sort_ic ? STRICMP(sortbuf1, sortbuf2) 350 : STRCMP(sortbuf1, sortbuf2); 351 } 352 353 /* If two lines have the same value, preserve the original line order. */ 354 if (result == 0) 355 return (int)(l1.lnum - l2.lnum); 356 return result; 357 } 358 359 /* 360 * ":sort". 361 */ 362 void 363 ex_sort(exarg_T *eap) 364 { 365 regmatch_T regmatch; 366 int len; 367 linenr_T lnum; 368 long maxlen = 0; 369 sorti_T *nrs; 370 size_t count = (size_t)(eap->line2 - eap->line1 + 1); 371 size_t i; 372 char_u *p; 373 char_u *s; 374 char_u *s2; 375 char_u c; /* temporary character storage */ 376 int unique = FALSE; 377 long deleted; 378 colnr_T start_col; 379 colnr_T end_col; 380 int sort_what = 0; 381 int format_found = 0; 382 383 /* Sorting one line is really quick! */ 384 if (count <= 1) 385 return; 386 387 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) 388 return; 389 sortbuf1 = NULL; 390 sortbuf2 = NULL; 391 regmatch.regprog = NULL; 392 nrs = (sorti_T *)lalloc((long_u)(count * sizeof(sorti_T)), TRUE); 393 if (nrs == NULL) 394 goto sortend; 395 396 sort_abort = sort_ic = sort_rx = sort_nr = 0; 397 #ifdef FEAT_FLOAT 398 sort_flt = 0; 399 #endif 400 401 for (p = eap->arg; *p != NUL; ++p) 402 { 403 if (vim_iswhite(*p)) 404 ; 405 else if (*p == 'i') 406 sort_ic = TRUE; 407 else if (*p == 'r') 408 sort_rx = TRUE; 409 else if (*p == 'n') 410 { 411 sort_nr = 1; 412 ++format_found; 413 } 414 #ifdef FEAT_FLOAT 415 else if (*p == 'f') 416 { 417 sort_flt = 1; 418 ++format_found; 419 } 420 #endif 421 else if (*p == 'b') 422 { 423 sort_what = STR2NR_BIN + STR2NR_FORCE; 424 ++format_found; 425 } 426 else if (*p == 'o') 427 { 428 sort_what = STR2NR_OCT + STR2NR_FORCE; 429 ++format_found; 430 } 431 else if (*p == 'x') 432 { 433 sort_what = STR2NR_HEX + STR2NR_FORCE; 434 ++format_found; 435 } 436 else if (*p == 'u') 437 unique = TRUE; 438 else if (*p == '"') /* comment start */ 439 break; 440 else if (check_nextcmd(p) != NULL) 441 { 442 eap->nextcmd = check_nextcmd(p); 443 break; 444 } 445 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) 446 { 447 s = skip_regexp(p + 1, *p, TRUE, NULL); 448 if (*s != *p) 449 { 450 EMSG(_(e_invalpat)); 451 goto sortend; 452 } 453 *s = NUL; 454 /* Use last search pattern if sort pattern is empty. */ 455 if (s == p + 1) 456 { 457 if (last_search_pat() == NULL) 458 { 459 EMSG(_(e_noprevre)); 460 goto sortend; 461 } 462 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); 463 } 464 else 465 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); 466 if (regmatch.regprog == NULL) 467 goto sortend; 468 p = s; /* continue after the regexp */ 469 regmatch.rm_ic = p_ic; 470 } 471 else 472 { 473 EMSG2(_(e_invarg2), p); 474 goto sortend; 475 } 476 } 477 478 /* Can only have one of 'n', 'b', 'o' and 'x'. */ 479 if (format_found > 1) 480 { 481 EMSG(_(e_invarg)); 482 goto sortend; 483 } 484 485 /* From here on "sort_nr" is used as a flag for any integer number 486 * sorting. */ 487 sort_nr += sort_what; 488 489 /* 490 * Make an array with all line numbers. This avoids having to copy all 491 * the lines into allocated memory. 492 * When sorting on strings "start_col_nr" is the offset in the line, for 493 * numbers sorting it's the number to sort on. This means the pattern 494 * matching and number conversion only has to be done once per line. 495 * Also get the longest line length for allocating "sortbuf". 496 */ 497 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) 498 { 499 s = ml_get(lnum); 500 len = (int)STRLEN(s); 501 if (maxlen < len) 502 maxlen = len; 503 504 start_col = 0; 505 end_col = len; 506 if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) 507 { 508 if (sort_rx) 509 { 510 start_col = (colnr_T)(regmatch.startp[0] - s); 511 end_col = (colnr_T)(regmatch.endp[0] - s); 512 } 513 else 514 start_col = (colnr_T)(regmatch.endp[0] - s); 515 } 516 else 517 if (regmatch.regprog != NULL) 518 end_col = 0; 519 520 if (sort_nr 521 #ifdef FEAT_FLOAT 522 || sort_flt 523 #endif 524 ) 525 { 526 /* Make sure vim_str2nr doesn't read any digits past the end 527 * of the match, by temporarily terminating the string there */ 528 s2 = s + end_col; 529 c = *s2; 530 *s2 = NUL; 531 /* Sorting on number: Store the number itself. */ 532 p = s + start_col; 533 if (sort_nr) 534 { 535 if (sort_what & STR2NR_HEX) 536 s = skiptohex(p); 537 else if (sort_what & STR2NR_BIN) 538 s = skiptobin(p); 539 else 540 s = skiptodigit(p); 541 if (s > p && s[-1] == '-') 542 --s; /* include preceding negative sign */ 543 if (*s == NUL) 544 /* empty line should sort before any number */ 545 nrs[lnum - eap->line1].st_u.value = -MAXLNUM; 546 else 547 vim_str2nr(s, NULL, NULL, sort_what, 548 &nrs[lnum - eap->line1].st_u.value, NULL, 0); 549 } 550 #ifdef FEAT_FLOAT 551 else 552 { 553 s = skipwhite(p); 554 if (*s == '+') 555 s = skipwhite(s + 1); 556 557 if (*s == NUL) 558 /* empty line should sort before any number */ 559 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX; 560 else 561 nrs[lnum - eap->line1].st_u.value_flt = 562 strtod((char *)s, NULL); 563 } 564 #endif 565 *s2 = c; 566 } 567 else 568 { 569 /* Store the column to sort at. */ 570 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col; 571 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col; 572 } 573 574 nrs[lnum - eap->line1].lnum = lnum; 575 576 if (regmatch.regprog != NULL) 577 fast_breakcheck(); 578 if (got_int) 579 goto sortend; 580 } 581 582 /* Allocate a buffer that can hold the longest line. */ 583 sortbuf1 = alloc((unsigned)maxlen + 1); 584 if (sortbuf1 == NULL) 585 goto sortend; 586 sortbuf2 = alloc((unsigned)maxlen + 1); 587 if (sortbuf2 == NULL) 588 goto sortend; 589 590 /* Sort the array of line numbers. Note: can't be interrupted! */ 591 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); 592 593 if (sort_abort) 594 goto sortend; 595 596 /* Insert the lines in the sorted order below the last one. */ 597 lnum = eap->line2; 598 for (i = 0; i < count; ++i) 599 { 600 s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum); 601 if (!unique || i == 0 602 || (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0) 603 { 604 /* Copy the line into a buffer, it may become invalid in 605 * ml_append(). And it's needed for "unique". */ 606 STRCPY(sortbuf1, s); 607 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL) 608 break; 609 } 610 fast_breakcheck(); 611 if (got_int) 612 goto sortend; 613 } 614 615 /* delete the original lines if appending worked */ 616 if (i == count) 617 for (i = 0; i < count; ++i) 618 ml_delete(eap->line1, FALSE); 619 else 620 count = 0; 621 622 /* Adjust marks for deleted (or added) lines and prepare for displaying. */ 623 deleted = (long)(count - (lnum - eap->line2)); 624 if (deleted > 0) 625 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); 626 else if (deleted < 0) 627 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); 628 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); 629 630 curwin->w_cursor.lnum = eap->line1; 631 beginline(BL_WHITE | BL_FIX); 632 633 sortend: 634 vim_free(nrs); 635 vim_free(sortbuf1); 636 vim_free(sortbuf2); 637 vim_regfree(regmatch.regprog); 638 if (got_int) 639 EMSG(_(e_interr)); 640 } 641 642 /* 643 * ":retab". 644 */ 645 void 646 ex_retab(exarg_T *eap) 647 { 648 linenr_T lnum; 649 int got_tab = FALSE; 650 long num_spaces = 0; 651 long num_tabs; 652 long len; 653 long col; 654 long vcol; 655 long start_col = 0; /* For start of white-space string */ 656 long start_vcol = 0; /* For start of white-space string */ 657 int temp; 658 long old_len; 659 char_u *ptr; 660 char_u *new_line = (char_u *)1; /* init to non-NULL */ 661 int did_undo; /* called u_save for current line */ 662 int new_ts; 663 int save_list; 664 linenr_T first_line = 0; /* first changed line */ 665 linenr_T last_line = 0; /* last changed line */ 666 667 save_list = curwin->w_p_list; 668 curwin->w_p_list = 0; /* don't want list mode here */ 669 670 new_ts = getdigits(&(eap->arg)); 671 if (new_ts < 0) 672 { 673 EMSG(_(e_positive)); 674 return; 675 } 676 if (new_ts == 0) 677 new_ts = curbuf->b_p_ts; 678 for (lnum = eap->line1; !got_int && lnum <= eap->line2; ++lnum) 679 { 680 ptr = ml_get(lnum); 681 col = 0; 682 vcol = 0; 683 did_undo = FALSE; 684 for (;;) 685 { 686 if (vim_iswhite(ptr[col])) 687 { 688 if (!got_tab && num_spaces == 0) 689 { 690 /* First consecutive white-space */ 691 start_vcol = vcol; 692 start_col = col; 693 } 694 if (ptr[col] == ' ') 695 num_spaces++; 696 else 697 got_tab = TRUE; 698 } 699 else 700 { 701 if (got_tab || (eap->forceit && num_spaces > 1)) 702 { 703 /* Retabulate this string of white-space */ 704 705 /* len is virtual length of white string */ 706 len = num_spaces = vcol - start_vcol; 707 num_tabs = 0; 708 if (!curbuf->b_p_et) 709 { 710 temp = new_ts - (start_vcol % new_ts); 711 if (num_spaces >= temp) 712 { 713 num_spaces -= temp; 714 num_tabs++; 715 } 716 num_tabs += num_spaces / new_ts; 717 num_spaces -= (num_spaces / new_ts) * new_ts; 718 } 719 if (curbuf->b_p_et || got_tab || 720 (num_spaces + num_tabs < len)) 721 { 722 if (did_undo == FALSE) 723 { 724 did_undo = TRUE; 725 if (u_save((linenr_T)(lnum - 1), 726 (linenr_T)(lnum + 1)) == FAIL) 727 { 728 new_line = NULL; /* flag out-of-memory */ 729 break; 730 } 731 } 732 733 /* len is actual number of white characters used */ 734 len = num_spaces + num_tabs; 735 old_len = (long)STRLEN(ptr); 736 new_line = lalloc(old_len - col + start_col + len + 1, 737 TRUE); 738 if (new_line == NULL) 739 break; 740 if (start_col > 0) 741 mch_memmove(new_line, ptr, (size_t)start_col); 742 mch_memmove(new_line + start_col + len, 743 ptr + col, (size_t)(old_len - col + 1)); 744 ptr = new_line + start_col; 745 for (col = 0; col < len; col++) 746 ptr[col] = (col < num_tabs) ? '\t' : ' '; 747 ml_replace(lnum, new_line, FALSE); 748 if (first_line == 0) 749 first_line = lnum; 750 last_line = lnum; 751 ptr = new_line; 752 col = start_col + len; 753 } 754 } 755 got_tab = FALSE; 756 num_spaces = 0; 757 } 758 if (ptr[col] == NUL) 759 break; 760 vcol += chartabsize(ptr + col, (colnr_T)vcol); 761 #ifdef FEAT_MBYTE 762 if (has_mbyte) 763 col += (*mb_ptr2len)(ptr + col); 764 else 765 #endif 766 ++col; 767 } 768 if (new_line == NULL) /* out of memory */ 769 break; 770 line_breakcheck(); 771 } 772 if (got_int) 773 EMSG(_(e_interr)); 774 775 if (curbuf->b_p_ts != new_ts) 776 redraw_curbuf_later(NOT_VALID); 777 if (first_line != 0) 778 changed_lines(first_line, 0, last_line + 1, 0L); 779 780 curwin->w_p_list = save_list; /* restore 'list' */ 781 782 curbuf->b_p_ts = new_ts; 783 coladvance(curwin->w_curswant); 784 785 u_clearline(); 786 } 787 788 /* 789 * :move command - move lines line1-line2 to line dest 790 * 791 * return FAIL for failure, OK otherwise 792 */ 793 int 794 do_move(linenr_T line1, linenr_T line2, linenr_T dest) 795 { 796 char_u *str; 797 linenr_T l; 798 linenr_T extra; /* Num lines added before line1 */ 799 linenr_T num_lines; /* Num lines moved */ 800 linenr_T last_line; /* Last line in file after adding new text */ 801 #ifdef FEAT_FOLDING 802 int isFolded; 803 804 /* Moving lines seems to corrupt the folds, delete folding info now 805 * and recreate it when finished. Don't do this for manual folding, it 806 * would delete all folds. */ 807 isFolded = hasAnyFolding(curwin) && !foldmethodIsManual(curwin); 808 if (isFolded) 809 deleteFoldRecurse(&curwin->w_folds); 810 #endif 811 812 if (dest >= line1 && dest < line2) 813 { 814 EMSG(_("E134: Move lines into themselves")); 815 return FAIL; 816 } 817 818 num_lines = line2 - line1 + 1; 819 820 /* 821 * First we copy the old text to its new location -- webb 822 * Also copy the flag that ":global" command uses. 823 */ 824 if (u_save(dest, dest + 1) == FAIL) 825 return FAIL; 826 for (extra = 0, l = line1; l <= line2; l++) 827 { 828 str = vim_strsave(ml_get(l + extra)); 829 if (str != NULL) 830 { 831 ml_append(dest + l - line1, str, (colnr_T)0, FALSE); 832 vim_free(str); 833 if (dest < line1) 834 extra++; 835 } 836 } 837 838 /* 839 * Now we must be careful adjusting our marks so that we don't overlap our 840 * mark_adjust() calls. 841 * 842 * We adjust the marks within the old text so that they refer to the 843 * last lines of the file (temporarily), because we know no other marks 844 * will be set there since these line numbers did not exist until we added 845 * our new lines. 846 * 847 * Then we adjust the marks on lines between the old and new text positions 848 * (either forwards or backwards). 849 * 850 * And Finally we adjust the marks we put at the end of the file back to 851 * their final destination at the new text position -- webb 852 */ 853 last_line = curbuf->b_ml.ml_line_count; 854 mark_adjust(line1, line2, last_line - line2, 0L); 855 changed_lines(last_line - num_lines + 1, 0, last_line + 1, num_lines); 856 if (dest >= line2) 857 { 858 mark_adjust(line2 + 1, dest, -num_lines, 0L); 859 curbuf->b_op_start.lnum = dest - num_lines + 1; 860 curbuf->b_op_end.lnum = dest; 861 } 862 else 863 { 864 mark_adjust(dest + 1, line1 - 1, num_lines, 0L); 865 curbuf->b_op_start.lnum = dest + 1; 866 curbuf->b_op_end.lnum = dest + num_lines; 867 } 868 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 869 mark_adjust(last_line - num_lines + 1, last_line, 870 -(last_line - dest - extra), 0L); 871 changed_lines(last_line - num_lines + 1, 0, last_line + 1, -extra); 872 873 /* 874 * Now we delete the original text -- webb 875 */ 876 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) 877 return FAIL; 878 879 for (l = line1; l <= line2; l++) 880 ml_delete(line1 + extra, TRUE); 881 882 if (!global_busy && num_lines > p_report) 883 { 884 if (num_lines == 1) 885 MSG(_("1 line moved")); 886 else 887 smsg((char_u *)_("%ld lines moved"), num_lines); 888 } 889 890 /* 891 * Leave the cursor on the last of the moved lines. 892 */ 893 if (dest >= line1) 894 curwin->w_cursor.lnum = dest; 895 else 896 curwin->w_cursor.lnum = dest + (line2 - line1) + 1; 897 898 if (line1 < dest) 899 { 900 dest += num_lines + 1; 901 last_line = curbuf->b_ml.ml_line_count; 902 if (dest > last_line + 1) 903 dest = last_line + 1; 904 changed_lines(line1, 0, dest, 0L); 905 } 906 else 907 changed_lines(dest + 1, 0, line1 + num_lines, 0L); 908 909 #ifdef FEAT_FOLDING 910 /* recreate folds */ 911 if (isFolded) 912 foldUpdateAll(curwin); 913 #endif 914 915 return OK; 916 } 917 918 /* 919 * ":copy" 920 */ 921 void 922 ex_copy(linenr_T line1, linenr_T line2, linenr_T n) 923 { 924 linenr_T count; 925 char_u *p; 926 927 count = line2 - line1 + 1; 928 curbuf->b_op_start.lnum = n + 1; 929 curbuf->b_op_end.lnum = n + count; 930 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 931 932 /* 933 * there are three situations: 934 * 1. destination is above line1 935 * 2. destination is between line1 and line2 936 * 3. destination is below line2 937 * 938 * n = destination (when starting) 939 * curwin->w_cursor.lnum = destination (while copying) 940 * line1 = start of source (while copying) 941 * line2 = end of source (while copying) 942 */ 943 if (u_save(n, n + 1) == FAIL) 944 return; 945 946 curwin->w_cursor.lnum = n; 947 while (line1 <= line2) 948 { 949 /* need to use vim_strsave() because the line will be unlocked within 950 * ml_append() */ 951 p = vim_strsave(ml_get(line1)); 952 if (p != NULL) 953 { 954 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); 955 vim_free(p); 956 } 957 /* situation 2: skip already copied lines */ 958 if (line1 == n) 959 line1 = curwin->w_cursor.lnum; 960 ++line1; 961 if (curwin->w_cursor.lnum < line1) 962 ++line1; 963 if (curwin->w_cursor.lnum < line2) 964 ++line2; 965 ++curwin->w_cursor.lnum; 966 } 967 968 appended_lines_mark(n, count); 969 970 msgmore((long)count); 971 } 972 973 static char_u *prevcmd = NULL; /* the previous command */ 974 975 #if defined(EXITFREE) || defined(PROTO) 976 void 977 free_prev_shellcmd(void) 978 { 979 vim_free(prevcmd); 980 } 981 #endif 982 983 /* 984 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" 985 * Bangs in the argument are replaced with the previously entered command. 986 * Remember the argument. 987 */ 988 void 989 do_bang( 990 int addr_count, 991 exarg_T *eap, 992 int forceit, 993 int do_in, 994 int do_out) 995 { 996 char_u *arg = eap->arg; /* command */ 997 linenr_T line1 = eap->line1; /* start of range */ 998 linenr_T line2 = eap->line2; /* end of range */ 999 char_u *newcmd = NULL; /* the new command */ 1000 int free_newcmd = FALSE; /* need to free() newcmd */ 1001 int ins_prevcmd; 1002 char_u *t; 1003 char_u *p; 1004 char_u *trailarg; 1005 int len; 1006 int scroll_save = msg_scroll; 1007 1008 /* 1009 * Disallow shell commands for "rvim". 1010 * Disallow shell commands from .exrc and .vimrc in current directory for 1011 * security reasons. 1012 */ 1013 if (check_restricted() || check_secure()) 1014 return; 1015 1016 if (addr_count == 0) /* :! */ 1017 { 1018 msg_scroll = FALSE; /* don't scroll here */ 1019 autowrite_all(); 1020 msg_scroll = scroll_save; 1021 } 1022 1023 /* 1024 * Try to find an embedded bang, like in :!<cmd> ! [args] 1025 * (:!! is indicated by the 'forceit' variable) 1026 */ 1027 ins_prevcmd = forceit; 1028 trailarg = arg; 1029 do 1030 { 1031 len = (int)STRLEN(trailarg) + 1; 1032 if (newcmd != NULL) 1033 len += (int)STRLEN(newcmd); 1034 if (ins_prevcmd) 1035 { 1036 if (prevcmd == NULL) 1037 { 1038 EMSG(_(e_noprev)); 1039 vim_free(newcmd); 1040 return; 1041 } 1042 len += (int)STRLEN(prevcmd); 1043 } 1044 if ((t = alloc((unsigned)len)) == NULL) 1045 { 1046 vim_free(newcmd); 1047 return; 1048 } 1049 *t = NUL; 1050 if (newcmd != NULL) 1051 STRCAT(t, newcmd); 1052 if (ins_prevcmd) 1053 STRCAT(t, prevcmd); 1054 p = t + STRLEN(t); 1055 STRCAT(t, trailarg); 1056 vim_free(newcmd); 1057 newcmd = t; 1058 1059 /* 1060 * Scan the rest of the argument for '!', which is replaced by the 1061 * previous command. "\!" is replaced by "!" (this is vi compatible). 1062 */ 1063 trailarg = NULL; 1064 while (*p) 1065 { 1066 if (*p == '!') 1067 { 1068 if (p > newcmd && p[-1] == '\\') 1069 STRMOVE(p - 1, p); 1070 else 1071 { 1072 trailarg = p; 1073 *trailarg++ = NUL; 1074 ins_prevcmd = TRUE; 1075 break; 1076 } 1077 } 1078 ++p; 1079 } 1080 } while (trailarg != NULL); 1081 1082 vim_free(prevcmd); 1083 prevcmd = newcmd; 1084 1085 if (bangredo) /* put cmd in redo buffer for ! command */ 1086 { 1087 /* If % or # appears in the command, it must have been escaped. 1088 * Reescape them, so that redoing them does not substitute them by the 1089 * buffername. */ 1090 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#"); 1091 1092 if (cmd != NULL) 1093 { 1094 AppendToRedobuffLit(cmd, -1); 1095 vim_free(cmd); 1096 } 1097 else 1098 AppendToRedobuffLit(prevcmd, -1); 1099 AppendToRedobuff((char_u *)"\n"); 1100 bangredo = FALSE; 1101 } 1102 /* 1103 * Add quotes around the command, for shells that need them. 1104 */ 1105 if (*p_shq != NUL) 1106 { 1107 newcmd = alloc((unsigned)(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1)); 1108 if (newcmd == NULL) 1109 return; 1110 STRCPY(newcmd, p_shq); 1111 STRCAT(newcmd, prevcmd); 1112 STRCAT(newcmd, p_shq); 1113 free_newcmd = TRUE; 1114 } 1115 if (addr_count == 0) /* :! */ 1116 { 1117 /* echo the command */ 1118 msg_start(); 1119 msg_putchar(':'); 1120 msg_putchar('!'); 1121 msg_outtrans(newcmd); 1122 msg_clr_eos(); 1123 windgoto(msg_row, msg_col); 1124 1125 do_shell(newcmd, 0); 1126 } 1127 else /* :range! */ 1128 { 1129 /* Careful: This may recursively call do_bang() again! (because of 1130 * autocommands) */ 1131 do_filter(line1, line2, eap, newcmd, do_in, do_out); 1132 #ifdef FEAT_AUTOCMD 1133 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); 1134 #endif 1135 } 1136 if (free_newcmd) 1137 vim_free(newcmd); 1138 } 1139 1140 /* 1141 * do_filter: filter lines through a command given by the user 1142 * 1143 * We mostly use temp files and the call_shell() routine here. This would 1144 * normally be done using pipes on a UNIX machine, but this is more portable 1145 * to non-unix machines. The call_shell() routine needs to be able 1146 * to deal with redirection somehow, and should handle things like looking 1147 * at the PATH env. variable, and adding reasonable extensions to the 1148 * command name given by the user. All reasonable versions of call_shell() 1149 * do this. 1150 * Alternatively, if on Unix and redirecting input or output, but not both, 1151 * and the 'shelltemp' option isn't set, use pipes. 1152 * We use input redirection if do_in is TRUE. 1153 * We use output redirection if do_out is TRUE. 1154 */ 1155 static void 1156 do_filter( 1157 linenr_T line1, 1158 linenr_T line2, 1159 exarg_T *eap, /* for forced 'ff' and 'fenc' */ 1160 char_u *cmd, 1161 int do_in, 1162 int do_out) 1163 { 1164 char_u *itmp = NULL; 1165 char_u *otmp = NULL; 1166 linenr_T linecount; 1167 linenr_T read_linecount; 1168 pos_T cursor_save; 1169 char_u *cmd_buf; 1170 #ifdef FEAT_AUTOCMD 1171 buf_T *old_curbuf = curbuf; 1172 #endif 1173 int shell_flags = 0; 1174 1175 if (*cmd == NUL) /* no filter command */ 1176 return; 1177 1178 #ifdef WIN3264 1179 /* 1180 * Check if external commands are allowed now. 1181 */ 1182 if (can_end_termcap_mode(TRUE) == FALSE) 1183 return; 1184 #endif 1185 1186 cursor_save = curwin->w_cursor; 1187 linecount = line2 - line1 + 1; 1188 curwin->w_cursor.lnum = line1; 1189 curwin->w_cursor.col = 0; 1190 changed_line_abv_curs(); 1191 invalidate_botline(); 1192 1193 /* 1194 * When using temp files: 1195 * 1. * Form temp file names 1196 * 2. * Write the lines to a temp file 1197 * 3. Run the filter command on the temp file 1198 * 4. * Read the output of the command into the buffer 1199 * 5. * Delete the original lines to be filtered 1200 * 6. * Remove the temp files 1201 * 1202 * When writing the input with a pipe or when catching the output with a 1203 * pipe only need to do 3. 1204 */ 1205 1206 if (do_out) 1207 shell_flags |= SHELL_DOOUT; 1208 1209 #ifdef FEAT_FILTERPIPE 1210 if (!do_in && do_out && !p_stmp) 1211 { 1212 /* Use a pipe to fetch stdout of the command, do not use a temp file. */ 1213 shell_flags |= SHELL_READ; 1214 curwin->w_cursor.lnum = line2; 1215 } 1216 else if (do_in && !do_out && !p_stmp) 1217 { 1218 /* Use a pipe to write stdin of the command, do not use a temp file. */ 1219 shell_flags |= SHELL_WRITE; 1220 curbuf->b_op_start.lnum = line1; 1221 curbuf->b_op_end.lnum = line2; 1222 } 1223 else if (do_in && do_out && !p_stmp) 1224 { 1225 /* Use a pipe to write stdin and fetch stdout of the command, do not 1226 * use a temp file. */ 1227 shell_flags |= SHELL_READ|SHELL_WRITE; 1228 curbuf->b_op_start.lnum = line1; 1229 curbuf->b_op_end.lnum = line2; 1230 curwin->w_cursor.lnum = line2; 1231 } 1232 else 1233 #endif 1234 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL) 1235 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL)) 1236 { 1237 EMSG(_(e_notmp)); 1238 goto filterend; 1239 } 1240 1241 /* 1242 * The writing and reading of temp files will not be shown. 1243 * Vi also doesn't do this and the messages are not very informative. 1244 */ 1245 ++no_wait_return; /* don't call wait_return() while busy */ 1246 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, 1247 FALSE, FALSE, FALSE, TRUE) == FAIL) 1248 { 1249 msg_putchar('\n'); /* keep message from buf_write() */ 1250 --no_wait_return; 1251 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1252 if (!aborting()) 1253 #endif 1254 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ 1255 goto filterend; 1256 } 1257 #ifdef FEAT_AUTOCMD 1258 if (curbuf != old_curbuf) 1259 goto filterend; 1260 #endif 1261 1262 if (!do_out) 1263 msg_putchar('\n'); 1264 1265 /* Create the shell command in allocated memory. */ 1266 cmd_buf = make_filter_cmd(cmd, itmp, otmp); 1267 if (cmd_buf == NULL) 1268 goto filterend; 1269 1270 windgoto((int)Rows - 1, 0); 1271 cursor_on(); 1272 1273 /* 1274 * When not redirecting the output the command can write anything to the 1275 * screen. If 'shellredir' is equal to ">", screen may be messed up by 1276 * stderr output of external command. Clear the screen later. 1277 * If do_in is FALSE, this could be something like ":r !cat", which may 1278 * also mess up the screen, clear it later. 1279 */ 1280 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) 1281 redraw_later_clear(); 1282 1283 if (do_out) 1284 { 1285 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) 1286 { 1287 vim_free(cmd_buf); 1288 goto error; 1289 } 1290 redraw_curbuf_later(VALID); 1291 } 1292 read_linecount = curbuf->b_ml.ml_line_count; 1293 1294 /* 1295 * When call_shell() fails wait_return() is called to give the user a 1296 * chance to read the error messages. Otherwise errors are ignored, so you 1297 * can see the error messages from the command that appear on stdout; use 1298 * 'u' to fix the text 1299 * Switch to cooked mode when not redirecting stdin, avoids that something 1300 * like ":r !cat" hangs. 1301 * Pass on the SHELL_DOOUT flag when the output is being redirected. 1302 */ 1303 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) 1304 { 1305 redraw_later_clear(); 1306 wait_return(FALSE); 1307 } 1308 vim_free(cmd_buf); 1309 1310 did_check_timestamps = FALSE; 1311 need_check_timestamps = TRUE; 1312 1313 /* When interrupting the shell command, it may still have produced some 1314 * useful output. Reset got_int here, so that readfile() won't cancel 1315 * reading. */ 1316 ui_breakcheck(); 1317 got_int = FALSE; 1318 1319 if (do_out) 1320 { 1321 if (otmp != NULL) 1322 { 1323 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, 1324 eap, READ_FILTER) == FAIL) 1325 { 1326 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1327 if (!aborting()) 1328 #endif 1329 { 1330 msg_putchar('\n'); 1331 EMSG2(_(e_notread), otmp); 1332 } 1333 goto error; 1334 } 1335 #ifdef FEAT_AUTOCMD 1336 if (curbuf != old_curbuf) 1337 goto filterend; 1338 #endif 1339 } 1340 1341 read_linecount = curbuf->b_ml.ml_line_count - read_linecount; 1342 1343 if (shell_flags & SHELL_READ) 1344 { 1345 curbuf->b_op_start.lnum = line2 + 1; 1346 curbuf->b_op_end.lnum = curwin->w_cursor.lnum; 1347 appended_lines_mark(line2, read_linecount); 1348 } 1349 1350 if (do_in) 1351 { 1352 if (cmdmod.keepmarks || vim_strchr(p_cpo, CPO_REMMARK) == NULL) 1353 { 1354 if (read_linecount >= linecount) 1355 /* move all marks from old lines to new lines */ 1356 mark_adjust(line1, line2, linecount, 0L); 1357 else 1358 { 1359 /* move marks from old lines to new lines, delete marks 1360 * that are in deleted lines */ 1361 mark_adjust(line1, line1 + read_linecount - 1, 1362 linecount, 0L); 1363 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); 1364 } 1365 } 1366 1367 /* 1368 * Put cursor on first filtered line for ":range!cmd". 1369 * Adjust '[ and '] (set by buf_write()). 1370 */ 1371 curwin->w_cursor.lnum = line1; 1372 del_lines(linecount, TRUE); 1373 curbuf->b_op_start.lnum -= linecount; /* adjust '[ */ 1374 curbuf->b_op_end.lnum -= linecount; /* adjust '] */ 1375 write_lnum_adjust(-linecount); /* adjust last line 1376 for next write */ 1377 #ifdef FEAT_FOLDING 1378 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); 1379 #endif 1380 } 1381 else 1382 { 1383 /* 1384 * Put cursor on last new line for ":r !cmd". 1385 */ 1386 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; 1387 curwin->w_cursor.lnum = curbuf->b_op_end.lnum; 1388 } 1389 1390 beginline(BL_WHITE | BL_FIX); /* cursor on first non-blank */ 1391 --no_wait_return; 1392 1393 if (linecount > p_report) 1394 { 1395 if (do_in) 1396 { 1397 vim_snprintf((char *)msg_buf, sizeof(msg_buf), 1398 _("%ld lines filtered"), (long)linecount); 1399 if (msg(msg_buf) && !msg_scroll) 1400 /* save message to display it after redraw */ 1401 set_keep_msg(msg_buf, 0); 1402 } 1403 else 1404 msgmore((long)linecount); 1405 } 1406 } 1407 else 1408 { 1409 error: 1410 /* put cursor back in same position for ":w !cmd" */ 1411 curwin->w_cursor = cursor_save; 1412 --no_wait_return; 1413 wait_return(FALSE); 1414 } 1415 1416 filterend: 1417 1418 #ifdef FEAT_AUTOCMD 1419 if (curbuf != old_curbuf) 1420 { 1421 --no_wait_return; 1422 EMSG(_("E135: *Filter* Autocommands must not change current buffer")); 1423 } 1424 #endif 1425 if (itmp != NULL) 1426 mch_remove(itmp); 1427 if (otmp != NULL) 1428 mch_remove(otmp); 1429 vim_free(itmp); 1430 vim_free(otmp); 1431 } 1432 1433 /* 1434 * Call a shell to execute a command. 1435 * When "cmd" is NULL start an interactive shell. 1436 */ 1437 void 1438 do_shell( 1439 char_u *cmd, 1440 int flags) /* may be SHELL_DOOUT when output is redirected */ 1441 { 1442 buf_T *buf; 1443 #ifndef FEAT_GUI_MSWIN 1444 int save_nwr; 1445 #endif 1446 #ifdef MSWIN 1447 int winstart = FALSE; 1448 #endif 1449 1450 /* 1451 * Disallow shell commands for "rvim". 1452 * Disallow shell commands from .exrc and .vimrc in current directory for 1453 * security reasons. 1454 */ 1455 if (check_restricted() || check_secure()) 1456 { 1457 msg_end(); 1458 return; 1459 } 1460 1461 #ifdef MSWIN 1462 /* 1463 * Check if external commands are allowed now. 1464 */ 1465 if (can_end_termcap_mode(TRUE) == FALSE) 1466 return; 1467 1468 /* 1469 * Check if ":!start" is used. 1470 */ 1471 if (cmd != NULL) 1472 winstart = (STRNICMP(cmd, "start ", 6) == 0); 1473 #endif 1474 1475 /* 1476 * For autocommands we want to get the output on the current screen, to 1477 * avoid having to type return below. 1478 */ 1479 msg_putchar('\r'); /* put cursor at start of line */ 1480 #ifdef FEAT_AUTOCMD 1481 if (!autocmd_busy) 1482 #endif 1483 { 1484 #ifdef MSWIN 1485 if (!winstart) 1486 #endif 1487 stoptermcap(); 1488 } 1489 #ifdef MSWIN 1490 if (!winstart) 1491 #endif 1492 msg_putchar('\n'); /* may shift screen one line up */ 1493 1494 /* warning message before calling the shell */ 1495 if (p_warn 1496 #ifdef FEAT_AUTOCMD 1497 && !autocmd_busy 1498 #endif 1499 && msg_silent == 0) 1500 for (buf = firstbuf; buf; buf = buf->b_next) 1501 if (bufIsChanged(buf)) 1502 { 1503 #ifdef FEAT_GUI_MSWIN 1504 if (!winstart) 1505 starttermcap(); /* don't want a message box here */ 1506 #endif 1507 MSG_PUTS(_("[No write since last change]\n")); 1508 #ifdef FEAT_GUI_MSWIN 1509 if (!winstart) 1510 stoptermcap(); 1511 #endif 1512 break; 1513 } 1514 1515 /* This windgoto is required for when the '\n' resulted in a "delete line 1516 * 1" command to the terminal. */ 1517 if (!swapping_screen()) 1518 windgoto(msg_row, msg_col); 1519 cursor_on(); 1520 (void)call_shell(cmd, SHELL_COOKED | flags); 1521 did_check_timestamps = FALSE; 1522 need_check_timestamps = TRUE; 1523 1524 /* 1525 * put the message cursor at the end of the screen, avoids wait_return() 1526 * to overwrite the text that the external command showed 1527 */ 1528 if (!swapping_screen()) 1529 { 1530 msg_row = Rows - 1; 1531 msg_col = 0; 1532 } 1533 1534 #ifdef FEAT_AUTOCMD 1535 if (autocmd_busy) 1536 { 1537 if (msg_silent == 0) 1538 redraw_later_clear(); 1539 } 1540 else 1541 #endif 1542 { 1543 /* 1544 * For ":sh" there is no need to call wait_return(), just redraw. 1545 * Also for the Win32 GUI (the output is in a console window). 1546 * Otherwise there is probably text on the screen that the user wants 1547 * to read before redrawing, so call wait_return(). 1548 */ 1549 #ifndef FEAT_GUI_MSWIN 1550 if (cmd == NULL 1551 # ifdef WIN3264 1552 || (winstart && !need_wait_return) 1553 # endif 1554 ) 1555 { 1556 if (msg_silent == 0) 1557 redraw_later_clear(); 1558 need_wait_return = FALSE; 1559 } 1560 else 1561 { 1562 /* 1563 * If we switch screens when starttermcap() is called, we really 1564 * want to wait for "hit return to continue". 1565 */ 1566 save_nwr = no_wait_return; 1567 if (swapping_screen()) 1568 no_wait_return = FALSE; 1569 # ifdef AMIGA 1570 wait_return(term_console ? -1 : msg_silent == 0); /* see below */ 1571 # else 1572 wait_return(msg_silent == 0); 1573 # endif 1574 no_wait_return = save_nwr; 1575 } 1576 #endif /* FEAT_GUI_W32 */ 1577 1578 #ifdef MSWIN 1579 if (!winstart) /* if winstart==TRUE, never stopped termcap! */ 1580 #endif 1581 starttermcap(); /* start termcap if not done by wait_return() */ 1582 1583 /* 1584 * In an Amiga window redrawing is caused by asking the window size. 1585 * If we got an interrupt this will not work. The chance that the 1586 * window size is wrong is very small, but we need to redraw the 1587 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY 1588 * but it saves an extra redraw. 1589 */ 1590 #ifdef AMIGA 1591 if (skip_redraw) /* ':' hit in wait_return() */ 1592 { 1593 if (msg_silent == 0) 1594 redraw_later_clear(); 1595 } 1596 else if (term_console) 1597 { 1598 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); /* get window size */ 1599 if (got_int && msg_silent == 0) 1600 redraw_later_clear(); /* if got_int is TRUE, redraw needed */ 1601 else 1602 must_redraw = 0; /* no extra redraw needed */ 1603 } 1604 #endif 1605 } 1606 1607 /* display any error messages now */ 1608 display_errors(); 1609 1610 #ifdef FEAT_AUTOCMD 1611 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); 1612 #endif 1613 } 1614 1615 /* 1616 * Create a shell command from a command string, input redirection file and 1617 * output redirection file. 1618 * Returns an allocated string with the shell command, or NULL for failure. 1619 */ 1620 char_u * 1621 make_filter_cmd( 1622 char_u *cmd, /* command */ 1623 char_u *itmp, /* NULL or name of input file */ 1624 char_u *otmp) /* NULL or name of output file */ 1625 { 1626 char_u *buf; 1627 long_u len; 1628 1629 #if defined(UNIX) 1630 int is_fish_shell; 1631 char_u *shell_name = get_isolated_shell_name(); 1632 1633 /* Account for fish's different syntax for subshells */ 1634 is_fish_shell = (fnamecmp(shell_name, "fish") == 0); 1635 vim_free(shell_name); 1636 if (is_fish_shell) 1637 len = (long_u)STRLEN(cmd) + 13; /* "begin; " + "; end" + NUL */ 1638 else 1639 #endif 1640 len = (long_u)STRLEN(cmd) + 3; /* "()" + NUL */ 1641 if (itmp != NULL) 1642 len += (long_u)STRLEN(itmp) + 9; /* " { < " + " } " */ 1643 if (otmp != NULL) 1644 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; /* " " */ 1645 buf = lalloc(len, TRUE); 1646 if (buf == NULL) 1647 return NULL; 1648 1649 #if defined(UNIX) 1650 /* 1651 * Put braces around the command (for concatenated commands) when 1652 * redirecting input and/or output. 1653 */ 1654 if (itmp != NULL || otmp != NULL) 1655 { 1656 if (is_fish_shell) 1657 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); 1658 else 1659 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); 1660 } 1661 else 1662 STRCPY(buf, cmd); 1663 if (itmp != NULL) 1664 { 1665 STRCAT(buf, " < "); 1666 STRCAT(buf, itmp); 1667 } 1668 #else 1669 /* 1670 * For shells that don't understand braces around commands, at least allow 1671 * the use of commands in a pipe. 1672 */ 1673 STRCPY(buf, cmd); 1674 if (itmp != NULL) 1675 { 1676 char_u *p; 1677 1678 /* 1679 * If there is a pipe, we have to put the '<' in front of it. 1680 * Don't do this when 'shellquote' is not empty, otherwise the 1681 * redirection would be inside the quotes. 1682 */ 1683 if (*p_shq == NUL) 1684 { 1685 p = vim_strchr(buf, '|'); 1686 if (p != NULL) 1687 *p = NUL; 1688 } 1689 STRCAT(buf, " <"); /* " < " causes problems on Amiga */ 1690 STRCAT(buf, itmp); 1691 if (*p_shq == NUL) 1692 { 1693 p = vim_strchr(cmd, '|'); 1694 if (p != NULL) 1695 { 1696 STRCAT(buf, " "); /* insert a space before the '|' for DOS */ 1697 STRCAT(buf, p); 1698 } 1699 } 1700 } 1701 #endif 1702 if (otmp != NULL) 1703 append_redir(buf, (int)len, p_srr, otmp); 1704 1705 return buf; 1706 } 1707 1708 /* 1709 * Append output redirection for file "fname" to the end of string buffer 1710 * "buf[buflen]" 1711 * Works with the 'shellredir' and 'shellpipe' options. 1712 * The caller should make sure that there is enough room: 1713 * STRLEN(opt) + STRLEN(fname) + 3 1714 */ 1715 void 1716 append_redir( 1717 char_u *buf, 1718 int buflen, 1719 char_u *opt, 1720 char_u *fname) 1721 { 1722 char_u *p; 1723 char_u *end; 1724 1725 end = buf + STRLEN(buf); 1726 /* find "%s" */ 1727 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) 1728 { 1729 if (p[1] == 's') /* found %s */ 1730 break; 1731 if (p[1] == '%') /* skip %% */ 1732 ++p; 1733 } 1734 if (p != NULL) 1735 { 1736 *end = ' '; /* not really needed? Not with sh, ksh or bash */ 1737 vim_snprintf((char *)end + 1, (size_t)(buflen - (end + 1 - buf)), 1738 (char *)opt, (char *)fname); 1739 } 1740 else 1741 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), 1742 #ifdef FEAT_QUICKFIX 1743 " %s %s", 1744 #else 1745 " %s%s", /* " > %s" causes problems on Amiga */ 1746 #endif 1747 (char *)opt, (char *)fname); 1748 } 1749 1750 #if defined(FEAT_VIMINFO) || defined(PROTO) 1751 1752 static int no_viminfo(void); 1753 static void write_viminfo_barlines(vir_T *virp, FILE *fp_out); 1754 static int viminfo_errcnt; 1755 1756 static int 1757 no_viminfo(void) 1758 { 1759 /* "vim -i NONE" does not read or write a viminfo file */ 1760 return (use_viminfo != NULL && STRCMP(use_viminfo, "NONE") == 0); 1761 } 1762 1763 /* 1764 * Report an error for reading a viminfo file. 1765 * Count the number of errors. When there are more than 10, return TRUE. 1766 */ 1767 int 1768 viminfo_error(char *errnum, char *message, char_u *line) 1769 { 1770 vim_snprintf((char *)IObuff, IOSIZE, _("%sviminfo: %s in line: "), 1771 errnum, message); 1772 STRNCAT(IObuff, line, IOSIZE - STRLEN(IObuff) - 1); 1773 if (IObuff[STRLEN(IObuff) - 1] == '\n') 1774 IObuff[STRLEN(IObuff) - 1] = NUL; 1775 emsg(IObuff); 1776 if (++viminfo_errcnt >= 10) 1777 { 1778 EMSG(_("E136: viminfo: Too many errors, skipping rest of file")); 1779 return TRUE; 1780 } 1781 return FALSE; 1782 } 1783 1784 /* 1785 * read_viminfo() -- Read the viminfo file. Registers etc. which are already 1786 * set are not over-written unless "flags" includes VIF_FORCEIT. -- webb 1787 */ 1788 int 1789 read_viminfo( 1790 char_u *file, /* file name or NULL to use default name */ 1791 int flags) /* VIF_WANT_INFO et al. */ 1792 { 1793 FILE *fp; 1794 char_u *fname; 1795 1796 if (no_viminfo()) 1797 return FAIL; 1798 1799 fname = viminfo_filename(file); /* get file name in allocated buffer */ 1800 if (fname == NULL) 1801 return FAIL; 1802 fp = mch_fopen((char *)fname, READBIN); 1803 1804 if (p_verbose > 0) 1805 { 1806 verbose_enter(); 1807 smsg((char_u *)_("Reading viminfo file \"%s\"%s%s%s"), 1808 fname, 1809 (flags & VIF_WANT_INFO) ? _(" info") : "", 1810 (flags & VIF_WANT_MARKS) ? _(" marks") : "", 1811 (flags & VIF_GET_OLDFILES) ? _(" oldfiles") : "", 1812 fp == NULL ? _(" FAILED") : ""); 1813 verbose_leave(); 1814 } 1815 1816 vim_free(fname); 1817 if (fp == NULL) 1818 return FAIL; 1819 1820 viminfo_errcnt = 0; 1821 do_viminfo(fp, NULL, flags); 1822 1823 fclose(fp); 1824 return OK; 1825 } 1826 1827 /* 1828 * Write the viminfo file. The old one is read in first so that effectively a 1829 * merge of current info and old info is done. This allows multiple vims to 1830 * run simultaneously, without losing any marks etc. 1831 * If "forceit" is TRUE, then the old file is not read in, and only internal 1832 * info is written to the file. 1833 */ 1834 void 1835 write_viminfo(char_u *file, int forceit) 1836 { 1837 char_u *fname; 1838 FILE *fp_in = NULL; /* input viminfo file, if any */ 1839 FILE *fp_out = NULL; /* output viminfo file */ 1840 char_u *tempname = NULL; /* name of temp viminfo file */ 1841 struct stat st_new; /* mch_stat() of potential new file */ 1842 char_u *wp; 1843 #if defined(UNIX) || defined(VMS) 1844 mode_t umask_save; 1845 #endif 1846 #ifdef UNIX 1847 int shortname = FALSE; /* use 8.3 file name */ 1848 struct stat st_old; /* mch_stat() of existing viminfo file */ 1849 #endif 1850 #ifdef WIN3264 1851 int hidden = FALSE; 1852 #endif 1853 1854 if (no_viminfo()) 1855 return; 1856 1857 fname = viminfo_filename(file); /* may set to default if NULL */ 1858 if (fname == NULL) 1859 return; 1860 1861 fp_in = mch_fopen((char *)fname, READBIN); 1862 if (fp_in == NULL) 1863 { 1864 /* if it does exist, but we can't read it, don't try writing */ 1865 if (mch_stat((char *)fname, &st_new) == 0) 1866 goto end; 1867 #if defined(UNIX) || defined(VMS) 1868 /* 1869 * For Unix we create the .viminfo non-accessible for others, 1870 * because it may contain text from non-accessible documents. 1871 */ 1872 umask_save = umask(077); 1873 #endif 1874 fp_out = mch_fopen((char *)fname, WRITEBIN); 1875 #if defined(UNIX) || defined(VMS) 1876 (void)umask(umask_save); 1877 #endif 1878 } 1879 else 1880 { 1881 /* 1882 * There is an existing viminfo file. Create a temporary file to 1883 * write the new viminfo into, in the same directory as the 1884 * existing viminfo file, which will be renamed later. 1885 */ 1886 #ifdef UNIX 1887 /* 1888 * For Unix we check the owner of the file. It's not very nice to 1889 * overwrite a user's viminfo file after a "su root", with a 1890 * viminfo file that the user can't read. 1891 */ 1892 st_old.st_dev = (dev_t)0; 1893 st_old.st_ino = 0; 1894 st_old.st_mode = 0600; 1895 if (mch_stat((char *)fname, &st_old) == 0 1896 && getuid() != ROOT_UID 1897 && !(st_old.st_uid == getuid() 1898 ? (st_old.st_mode & 0200) 1899 : (st_old.st_gid == getgid() 1900 ? (st_old.st_mode & 0020) 1901 : (st_old.st_mode & 0002)))) 1902 { 1903 int tt = msg_didany; 1904 1905 /* avoid a wait_return for this message, it's annoying */ 1906 EMSG2(_("E137: Viminfo file is not writable: %s"), fname); 1907 msg_didany = tt; 1908 fclose(fp_in); 1909 goto end; 1910 } 1911 #endif 1912 #ifdef WIN3264 1913 /* Get the file attributes of the existing viminfo file. */ 1914 hidden = mch_ishidden(fname); 1915 #endif 1916 1917 /* 1918 * Make tempname. 1919 * May try twice: Once normal and once with shortname set, just in 1920 * case somebody puts his viminfo file in an 8.3 filesystem. 1921 */ 1922 for (;;) 1923 { 1924 tempname = buf_modname( 1925 #ifdef UNIX 1926 shortname, 1927 #else 1928 # ifdef FEAT_GUI_W32 1929 gui_is_win32s(), 1930 # else 1931 FALSE, 1932 # endif 1933 #endif 1934 fname, 1935 #ifdef VMS 1936 (char_u *)"-tmp", 1937 #else 1938 (char_u *)".tmp", 1939 #endif 1940 FALSE); 1941 if (tempname == NULL) /* out of memory */ 1942 break; 1943 1944 /* 1945 * Check if tempfile already exists. Never overwrite an 1946 * existing file! 1947 */ 1948 if (mch_stat((char *)tempname, &st_new) == 0) 1949 { 1950 #ifdef UNIX 1951 /* 1952 * Check if tempfile is same as original file. May happen 1953 * when modname() gave the same file back. E.g. silly 1954 * link, or file name-length reached. Try again with 1955 * shortname set. 1956 */ 1957 if (!shortname && st_new.st_dev == st_old.st_dev 1958 && st_new.st_ino == st_old.st_ino) 1959 { 1960 vim_free(tempname); 1961 tempname = NULL; 1962 shortname = TRUE; 1963 continue; 1964 } 1965 #endif 1966 /* 1967 * Try another name. Change one character, just before 1968 * the extension. This should also work for an 8.3 1969 * file name, when after adding the extension it still is 1970 * the same file as the original. 1971 */ 1972 wp = tempname + STRLEN(tempname) - 5; 1973 if (wp < gettail(tempname)) /* empty file name? */ 1974 wp = gettail(tempname); 1975 for (*wp = 'z'; mch_stat((char *)tempname, &st_new) == 0; 1976 --*wp) 1977 { 1978 /* 1979 * They all exist? Must be something wrong! Don't 1980 * write the viminfo file then. 1981 */ 1982 if (*wp == 'a') 1983 { 1984 vim_free(tempname); 1985 tempname = NULL; 1986 break; 1987 } 1988 } 1989 } 1990 break; 1991 } 1992 1993 if (tempname != NULL) 1994 { 1995 #ifdef VMS 1996 /* fdopen() fails for some reason */ 1997 umask_save = umask(077); 1998 fp_out = mch_fopen((char *)tempname, WRITEBIN); 1999 (void)umask(umask_save); 2000 #else 2001 int fd; 2002 2003 /* Use mch_open() to be able to use O_NOFOLLOW and set file 2004 * protection: 2005 * Unix: same as original file, but strip s-bit. Reset umask to 2006 * avoid it getting in the way. 2007 * Others: r&w for user only. */ 2008 # ifdef UNIX 2009 umask_save = umask(0); 2010 fd = mch_open((char *)tempname, 2011 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 2012 (int)((st_old.st_mode & 0777) | 0600)); 2013 (void)umask(umask_save); 2014 # else 2015 fd = mch_open((char *)tempname, 2016 O_CREAT|O_EXTRA|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600); 2017 # endif 2018 if (fd < 0) 2019 fp_out = NULL; 2020 else 2021 fp_out = fdopen(fd, WRITEBIN); 2022 #endif /* VMS */ 2023 2024 /* 2025 * If we can't create in the same directory, try creating a 2026 * "normal" temp file. 2027 */ 2028 if (fp_out == NULL) 2029 { 2030 vim_free(tempname); 2031 if ((tempname = vim_tempname('o', TRUE)) != NULL) 2032 fp_out = mch_fopen((char *)tempname, WRITEBIN); 2033 } 2034 2035 #if defined(UNIX) && defined(HAVE_FCHOWN) 2036 /* 2037 * Make sure the owner can read/write it. This only works for 2038 * root. 2039 */ 2040 if (fp_out != NULL) 2041 ignored = fchown(fileno(fp_out), st_old.st_uid, st_old.st_gid); 2042 #endif 2043 } 2044 } 2045 2046 /* 2047 * Check if the new viminfo file can be written to. 2048 */ 2049 if (fp_out == NULL) 2050 { 2051 EMSG2(_("E138: Can't write viminfo file %s!"), 2052 (fp_in == NULL || tempname == NULL) ? fname : tempname); 2053 if (fp_in != NULL) 2054 fclose(fp_in); 2055 goto end; 2056 } 2057 2058 if (p_verbose > 0) 2059 { 2060 verbose_enter(); 2061 smsg((char_u *)_("Writing viminfo file \"%s\""), fname); 2062 verbose_leave(); 2063 } 2064 2065 viminfo_errcnt = 0; 2066 do_viminfo(fp_in, fp_out, forceit ? 0 : (VIF_WANT_INFO | VIF_WANT_MARKS)); 2067 2068 if (fclose(fp_out) == EOF) 2069 ++viminfo_errcnt; 2070 2071 if (fp_in != NULL) 2072 { 2073 fclose(fp_in); 2074 2075 /* In case of an error keep the original viminfo file. Otherwise 2076 * rename the newly written file. Give an error if that fails. */ 2077 if (viminfo_errcnt == 0) 2078 { 2079 if (vim_rename(tempname, fname) == -1) 2080 { 2081 ++viminfo_errcnt; 2082 EMSG2(_("E886: Can't rename viminfo file to %s!"), fname); 2083 } 2084 # ifdef WIN3264 2085 /* If the viminfo file was hidden then also hide the new file. */ 2086 else if (hidden) 2087 mch_hide(fname); 2088 # endif 2089 } 2090 if (viminfo_errcnt > 0) 2091 mch_remove(tempname); 2092 } 2093 2094 end: 2095 vim_free(fname); 2096 vim_free(tempname); 2097 } 2098 2099 /* 2100 * Get the viminfo file name to use. 2101 * If "file" is given and not empty, use it (has already been expanded by 2102 * cmdline functions). 2103 * Otherwise use "-i file_name", value from 'viminfo' or the default, and 2104 * expand environment variables. 2105 * Returns an allocated string. NULL when out of memory. 2106 */ 2107 static char_u * 2108 viminfo_filename(char_u *file) 2109 { 2110 if (file == NULL || *file == NUL) 2111 { 2112 if (use_viminfo != NULL) 2113 file = use_viminfo; 2114 else if ((file = find_viminfo_parameter('n')) == NULL || *file == NUL) 2115 { 2116 #ifdef VIMINFO_FILE2 2117 /* don't use $HOME when not defined (turned into "c:/"!). */ 2118 # ifdef VMS 2119 if (mch_getenv((char_u *)"SYS$LOGIN") == NULL) 2120 # else 2121 if (mch_getenv((char_u *)"HOME") == NULL) 2122 # endif 2123 { 2124 /* don't use $VIM when not available. */ 2125 expand_env((char_u *)"$VIM", NameBuff, MAXPATHL); 2126 if (STRCMP("$VIM", NameBuff) != 0) /* $VIM was expanded */ 2127 file = (char_u *)VIMINFO_FILE2; 2128 else 2129 file = (char_u *)VIMINFO_FILE; 2130 } 2131 else 2132 #endif 2133 file = (char_u *)VIMINFO_FILE; 2134 } 2135 expand_env(file, NameBuff, MAXPATHL); 2136 file = NameBuff; 2137 } 2138 return vim_strsave(file); 2139 } 2140 2141 /* 2142 * do_viminfo() -- Should only be called from read_viminfo() & write_viminfo(). 2143 */ 2144 static void 2145 do_viminfo(FILE *fp_in, FILE *fp_out, int flags) 2146 { 2147 int count = 0; 2148 int eof = FALSE; 2149 vir_T vir; 2150 int merge = FALSE; 2151 2152 if ((vir.vir_line = alloc(LSIZE)) == NULL) 2153 return; 2154 vir.vir_fd = fp_in; 2155 #ifdef FEAT_MBYTE 2156 vir.vir_conv.vc_type = CONV_NONE; 2157 #endif 2158 ga_init2(&vir.vir_barlines, (int)sizeof(char_u *), 100); 2159 2160 if (fp_in != NULL) 2161 { 2162 if (flags & VIF_WANT_INFO) 2163 { 2164 eof = read_viminfo_up_to_marks(&vir, 2165 flags & VIF_FORCEIT, fp_out != NULL); 2166 merge = TRUE; 2167 } 2168 else if (flags != 0) 2169 /* Skip info, find start of marks */ 2170 while (!(eof = viminfo_readline(&vir)) 2171 && vir.vir_line[0] != '>') 2172 ; 2173 } 2174 if (fp_out != NULL) 2175 { 2176 /* Write the info: */ 2177 fprintf(fp_out, _("# This viminfo file was generated by Vim %s.\n"), 2178 VIM_VERSION_MEDIUM); 2179 fputs(_("# You may edit it if you're careful!\n\n"), fp_out); 2180 #ifdef FEAT_MBYTE 2181 fputs(_("# Value of 'encoding' when this file was written\n"), fp_out); 2182 fprintf(fp_out, "*encoding=%s\n\n", p_enc); 2183 #endif 2184 write_viminfo_search_pattern(fp_out); 2185 write_viminfo_sub_string(fp_out); 2186 #ifdef FEAT_CMDHIST 2187 write_viminfo_history(fp_out, merge); 2188 #endif 2189 write_viminfo_registers(fp_out); 2190 #ifdef FEAT_EVAL 2191 write_viminfo_varlist(fp_out); 2192 #endif 2193 write_viminfo_filemarks(fp_out); 2194 write_viminfo_bufferlist(fp_out); 2195 write_viminfo_barlines(&vir, fp_out); 2196 count = write_viminfo_marks(fp_out); 2197 } 2198 if (fp_in != NULL 2199 && (flags & (VIF_WANT_MARKS | VIF_GET_OLDFILES | VIF_FORCEIT))) 2200 copy_viminfo_marks(&vir, fp_out, count, eof, flags); 2201 2202 vim_free(vir.vir_line); 2203 #ifdef FEAT_MBYTE 2204 if (vir.vir_conv.vc_type != CONV_NONE) 2205 convert_setup(&vir.vir_conv, NULL, NULL); 2206 #endif 2207 ga_clear_strings(&vir.vir_barlines); 2208 } 2209 2210 /* 2211 * read_viminfo_up_to_marks() -- Only called from do_viminfo(). Reads in the 2212 * first part of the viminfo file which contains everything but the marks that 2213 * are local to a file. Returns TRUE when end-of-file is reached. -- webb 2214 */ 2215 static int 2216 read_viminfo_up_to_marks( 2217 vir_T *virp, 2218 int forceit, 2219 int writing) 2220 { 2221 int eof; 2222 buf_T *buf; 2223 2224 #ifdef FEAT_CMDHIST 2225 prepare_viminfo_history(forceit ? 9999 : 0, writing); 2226 #endif 2227 eof = viminfo_readline(virp); 2228 while (!eof && virp->vir_line[0] != '>') 2229 { 2230 switch (virp->vir_line[0]) 2231 { 2232 /* Characters reserved for future expansion, ignored now */ 2233 case '+': /* "+40 /path/dir file", for running vim without args */ 2234 case '^': /* to be defined */ 2235 case '<': /* long line - ignored */ 2236 /* A comment or empty line. */ 2237 case NUL: 2238 case '\r': 2239 case '\n': 2240 case '#': 2241 eof = viminfo_readline(virp); 2242 break; 2243 case '|': /* copy line (for future use) */ 2244 if (writing) 2245 ga_add_string(&virp->vir_barlines, virp->vir_line); 2246 eof = viminfo_readline(virp); 2247 break; 2248 case '*': /* "*encoding=value" */ 2249 eof = viminfo_encoding(virp); 2250 break; 2251 case '!': /* global variable */ 2252 #ifdef FEAT_EVAL 2253 eof = read_viminfo_varlist(virp, writing); 2254 #else 2255 eof = viminfo_readline(virp); 2256 #endif 2257 break; 2258 case '%': /* entry for buffer list */ 2259 eof = read_viminfo_bufferlist(virp, writing); 2260 break; 2261 case '"': 2262 eof = read_viminfo_register(virp, forceit); 2263 break; 2264 case '/': /* Search string */ 2265 case '&': /* Substitute search string */ 2266 case '~': /* Last search string, followed by '/' or '&' */ 2267 eof = read_viminfo_search_pattern(virp, forceit); 2268 break; 2269 case '$': 2270 eof = read_viminfo_sub_string(virp, forceit); 2271 break; 2272 case ':': 2273 case '?': 2274 case '=': 2275 case '@': 2276 #ifdef FEAT_CMDHIST 2277 eof = read_viminfo_history(virp, writing); 2278 #else 2279 eof = viminfo_readline(virp); 2280 #endif 2281 break; 2282 case '-': 2283 case '\'': 2284 eof = read_viminfo_filemark(virp, forceit); 2285 break; 2286 default: 2287 if (viminfo_error("E575: ", _("Illegal starting char"), 2288 virp->vir_line)) 2289 eof = TRUE; 2290 else 2291 eof = viminfo_readline(virp); 2292 break; 2293 } 2294 } 2295 2296 #ifdef FEAT_CMDHIST 2297 /* Finish reading history items. */ 2298 if (!writing) 2299 finish_viminfo_history(); 2300 #endif 2301 2302 /* Change file names to buffer numbers for fmarks. */ 2303 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2304 fmarks_check_names(buf); 2305 2306 return eof; 2307 } 2308 2309 /* 2310 * Compare the 'encoding' value in the viminfo file with the current value of 2311 * 'encoding'. If different and the 'c' flag is in 'viminfo', setup for 2312 * conversion of text with iconv() in viminfo_readstring(). 2313 */ 2314 static int 2315 viminfo_encoding(vir_T *virp) 2316 { 2317 #ifdef FEAT_MBYTE 2318 char_u *p; 2319 int i; 2320 2321 if (get_viminfo_parameter('c') != 0) 2322 { 2323 p = vim_strchr(virp->vir_line, '='); 2324 if (p != NULL) 2325 { 2326 /* remove trailing newline */ 2327 ++p; 2328 for (i = 0; vim_isprintc(p[i]); ++i) 2329 ; 2330 p[i] = NUL; 2331 2332 convert_setup(&virp->vir_conv, p, p_enc); 2333 } 2334 } 2335 #endif 2336 return viminfo_readline(virp); 2337 } 2338 2339 /* 2340 * Read a line from the viminfo file. 2341 * Returns TRUE for end-of-file; 2342 */ 2343 int 2344 viminfo_readline(vir_T *virp) 2345 { 2346 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); 2347 } 2348 2349 /* 2350 * check string read from viminfo file 2351 * remove '\n' at the end of the line 2352 * - replace CTRL-V CTRL-V with CTRL-V 2353 * - replace CTRL-V 'n' with '\n' 2354 * 2355 * Check for a long line as written by viminfo_writestring(). 2356 * 2357 * Return the string in allocated memory (NULL when out of memory). 2358 */ 2359 char_u * 2360 viminfo_readstring( 2361 vir_T *virp, 2362 int off, /* offset for virp->vir_line */ 2363 int convert UNUSED) /* convert the string */ 2364 { 2365 char_u *retval; 2366 char_u *s, *d; 2367 long len; 2368 2369 if (virp->vir_line[off] == Ctrl_V && vim_isdigit(virp->vir_line[off + 1])) 2370 { 2371 len = atol((char *)virp->vir_line + off + 1); 2372 retval = lalloc(len, TRUE); 2373 if (retval == NULL) 2374 { 2375 /* Line too long? File messed up? Skip next line. */ 2376 (void)vim_fgets(virp->vir_line, 10, virp->vir_fd); 2377 return NULL; 2378 } 2379 (void)vim_fgets(retval, (int)len, virp->vir_fd); 2380 s = retval + 1; /* Skip the leading '<' */ 2381 } 2382 else 2383 { 2384 retval = vim_strsave(virp->vir_line + off); 2385 if (retval == NULL) 2386 return NULL; 2387 s = retval; 2388 } 2389 2390 /* Change CTRL-V CTRL-V to CTRL-V and CTRL-V n to \n in-place. */ 2391 d = retval; 2392 while (*s != NUL && *s != '\n') 2393 { 2394 if (s[0] == Ctrl_V && s[1] != NUL) 2395 { 2396 if (s[1] == 'n') 2397 *d++ = '\n'; 2398 else 2399 *d++ = Ctrl_V; 2400 s += 2; 2401 } 2402 else 2403 *d++ = *s++; 2404 } 2405 *d = NUL; 2406 2407 #ifdef FEAT_MBYTE 2408 if (convert && virp->vir_conv.vc_type != CONV_NONE && *retval != NUL) 2409 { 2410 d = string_convert(&virp->vir_conv, retval, NULL); 2411 if (d != NULL) 2412 { 2413 vim_free(retval); 2414 retval = d; 2415 } 2416 } 2417 #endif 2418 2419 return retval; 2420 } 2421 2422 /* 2423 * write string to viminfo file 2424 * - replace CTRL-V with CTRL-V CTRL-V 2425 * - replace '\n' with CTRL-V 'n' 2426 * - add a '\n' at the end 2427 * 2428 * For a long line: 2429 * - write " CTRL-V <length> \n " in first line 2430 * - write " < <string> \n " in second line 2431 */ 2432 void 2433 viminfo_writestring(FILE *fd, char_u *p) 2434 { 2435 int c; 2436 char_u *s; 2437 int len = 0; 2438 2439 for (s = p; *s != NUL; ++s) 2440 { 2441 if (*s == Ctrl_V || *s == '\n') 2442 ++len; 2443 ++len; 2444 } 2445 2446 /* If the string will be too long, write its length and put it in the next 2447 * line. Take into account that some room is needed for what comes before 2448 * the string (e.g., variable name). Add something to the length for the 2449 * '<', NL and trailing NUL. */ 2450 if (len > LSIZE / 2) 2451 fprintf(fd, IF_EB("\026%d\n<", CTRL_V_STR "%d\n<"), len + 3); 2452 2453 while ((c = *p++) != NUL) 2454 { 2455 if (c == Ctrl_V || c == '\n') 2456 { 2457 putc(Ctrl_V, fd); 2458 if (c == '\n') 2459 c = 'n'; 2460 } 2461 putc(c, fd); 2462 } 2463 putc('\n', fd); 2464 } 2465 2466 static void 2467 write_viminfo_barlines(vir_T *virp, FILE *fp_out) 2468 { 2469 int i; 2470 garray_T *gap = &virp->vir_barlines; 2471 2472 if (gap->ga_len > 0) 2473 { 2474 fputs(_("\n# Bar lines, copied verbatim:\n"), fp_out); 2475 2476 for (i = 0; i < gap->ga_len; ++i) 2477 fputs(((char **)(gap->ga_data))[i], fp_out); 2478 } 2479 } 2480 #endif /* FEAT_VIMINFO */ 2481 2482 /* 2483 * Implementation of ":fixdel", also used by get_stty(). 2484 * <BS> resulting <Del> 2485 * ^? ^H 2486 * not ^? ^? 2487 */ 2488 void 2489 do_fixdel(exarg_T *eap UNUSED) 2490 { 2491 char_u *p; 2492 2493 p = find_termcode((char_u *)"kb"); 2494 add_termcode((char_u *)"kD", p != NULL 2495 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); 2496 } 2497 2498 void 2499 print_line_no_prefix( 2500 linenr_T lnum, 2501 int use_number, 2502 int list) 2503 { 2504 char_u numbuf[30]; 2505 2506 if (curwin->w_p_nu || use_number) 2507 { 2508 vim_snprintf((char *)numbuf, sizeof(numbuf), 2509 "%*ld ", number_width(curwin), (long)lnum); 2510 msg_puts_attr(numbuf, hl_attr(HLF_N)); /* Highlight line nrs */ 2511 } 2512 msg_prt_line(ml_get(lnum), list); 2513 } 2514 2515 /* 2516 * Print a text line. Also in silent mode ("ex -s"). 2517 */ 2518 void 2519 print_line(linenr_T lnum, int use_number, int list) 2520 { 2521 int save_silent = silent_mode; 2522 2523 msg_start(); 2524 silent_mode = FALSE; 2525 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ 2526 print_line_no_prefix(lnum, use_number, list); 2527 if (save_silent) 2528 { 2529 msg_putchar('\n'); 2530 cursor_on(); /* msg_start() switches it off */ 2531 out_flush(); 2532 silent_mode = save_silent; 2533 } 2534 info_message = FALSE; 2535 } 2536 2537 int 2538 rename_buffer(char_u *new_fname) 2539 { 2540 char_u *fname, *sfname, *xfname; 2541 buf_T *buf; 2542 2543 #ifdef FEAT_AUTOCMD 2544 buf = curbuf; 2545 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 2546 /* buffer changed, don't change name now */ 2547 if (buf != curbuf) 2548 return FAIL; 2549 # ifdef FEAT_EVAL 2550 if (aborting()) /* autocmds may abort script processing */ 2551 return FAIL; 2552 # endif 2553 #endif 2554 /* 2555 * The name of the current buffer will be changed. 2556 * A new (unlisted) buffer entry needs to be made to hold the old file 2557 * name, which will become the alternate file name. 2558 * But don't set the alternate file name if the buffer didn't have a 2559 * name. 2560 */ 2561 fname = curbuf->b_ffname; 2562 sfname = curbuf->b_sfname; 2563 xfname = curbuf->b_fname; 2564 curbuf->b_ffname = NULL; 2565 curbuf->b_sfname = NULL; 2566 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL) 2567 { 2568 curbuf->b_ffname = fname; 2569 curbuf->b_sfname = sfname; 2570 return FAIL; 2571 } 2572 curbuf->b_flags |= BF_NOTEDITED; 2573 if (xfname != NULL && *xfname != NUL) 2574 { 2575 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); 2576 if (buf != NULL && !cmdmod.keepalt) 2577 curwin->w_alt_fnum = buf->b_fnum; 2578 } 2579 vim_free(fname); 2580 vim_free(sfname); 2581 #ifdef FEAT_AUTOCMD 2582 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 2583 #endif 2584 /* Change directories when the 'acd' option is set. */ 2585 DO_AUTOCHDIR 2586 return OK; 2587 } 2588 2589 /* 2590 * ":file[!] [fname]". 2591 */ 2592 void 2593 ex_file(exarg_T *eap) 2594 { 2595 /* ":0file" removes the file name. Check for illegal uses ":3file", 2596 * "0file name", etc. */ 2597 if (eap->addr_count > 0 2598 && (*eap->arg != NUL 2599 || eap->line2 > 0 2600 || eap->addr_count > 1)) 2601 { 2602 EMSG(_(e_invarg)); 2603 return; 2604 } 2605 2606 if (*eap->arg != NUL || eap->addr_count == 1) 2607 { 2608 if (rename_buffer(eap->arg) == FAIL) 2609 return; 2610 } 2611 /* print full file name if :cd used */ 2612 if (!shortmess(SHM_FILEINFO)) 2613 fileinfo(FALSE, FALSE, eap->forceit); 2614 } 2615 2616 /* 2617 * ":update". 2618 */ 2619 void 2620 ex_update(exarg_T *eap) 2621 { 2622 if (curbufIsChanged()) 2623 (void)do_write(eap); 2624 } 2625 2626 /* 2627 * ":write" and ":saveas". 2628 */ 2629 void 2630 ex_write(exarg_T *eap) 2631 { 2632 if (eap->usefilter) /* input lines to shell command */ 2633 do_bang(1, eap, FALSE, TRUE, FALSE); 2634 else 2635 (void)do_write(eap); 2636 } 2637 2638 /* 2639 * write current buffer to file 'eap->arg' 2640 * if 'eap->append' is TRUE, append to the file 2641 * 2642 * if *eap->arg == NUL write to current file 2643 * 2644 * return FAIL for failure, OK otherwise 2645 */ 2646 int 2647 do_write(exarg_T *eap) 2648 { 2649 int other; 2650 char_u *fname = NULL; /* init to shut up gcc */ 2651 char_u *ffname; 2652 int retval = FAIL; 2653 char_u *free_fname = NULL; 2654 #ifdef FEAT_BROWSE 2655 char_u *browse_file = NULL; 2656 #endif 2657 buf_T *alt_buf = NULL; 2658 2659 if (not_writing()) /* check 'write' option */ 2660 return FAIL; 2661 2662 ffname = eap->arg; 2663 #ifdef FEAT_BROWSE 2664 if (cmdmod.browse) 2665 { 2666 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, 2667 NULL, NULL, NULL, curbuf); 2668 if (browse_file == NULL) 2669 goto theend; 2670 ffname = browse_file; 2671 } 2672 #endif 2673 if (*ffname == NUL) 2674 { 2675 if (eap->cmdidx == CMD_saveas) 2676 { 2677 EMSG(_(e_argreq)); 2678 goto theend; 2679 } 2680 other = FALSE; 2681 } 2682 else 2683 { 2684 fname = ffname; 2685 free_fname = fix_fname(ffname); 2686 /* 2687 * When out-of-memory, keep unexpanded file name, because we MUST be 2688 * able to write the file in this situation. 2689 */ 2690 if (free_fname != NULL) 2691 ffname = free_fname; 2692 other = otherfile(ffname); 2693 } 2694 2695 /* 2696 * If we have a new file, put its name in the list of alternate file names. 2697 */ 2698 if (other) 2699 { 2700 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL 2701 || eap->cmdidx == CMD_saveas) 2702 alt_buf = setaltfname(ffname, fname, (linenr_T)1); 2703 else 2704 alt_buf = buflist_findname(ffname); 2705 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) 2706 { 2707 /* Overwriting a file that is loaded in another buffer is not a 2708 * good idea. */ 2709 EMSG(_(e_bufloaded)); 2710 goto theend; 2711 } 2712 } 2713 2714 /* 2715 * Writing to the current file is not allowed in readonly mode 2716 * and a file name is required. 2717 * "nofile" and "nowrite" buffers cannot be written implicitly either. 2718 */ 2719 if (!other && ( 2720 #ifdef FEAT_QUICKFIX 2721 bt_dontwrite_msg(curbuf) || 2722 #endif 2723 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) 2724 goto theend; 2725 2726 if (!other) 2727 { 2728 ffname = curbuf->b_ffname; 2729 fname = curbuf->b_fname; 2730 /* 2731 * Not writing the whole file is only allowed with '!'. 2732 */ 2733 if ( (eap->line1 != 1 2734 || eap->line2 != curbuf->b_ml.ml_line_count) 2735 && !eap->forceit 2736 && !eap->append 2737 && !p_wa) 2738 { 2739 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2740 if (p_confirm || cmdmod.confirm) 2741 { 2742 if (vim_dialog_yesno(VIM_QUESTION, NULL, 2743 (char_u *)_("Write partial file?"), 2) != VIM_YES) 2744 goto theend; 2745 eap->forceit = TRUE; 2746 } 2747 else 2748 #endif 2749 { 2750 EMSG(_("E140: Use ! to write partial buffer")); 2751 goto theend; 2752 } 2753 } 2754 } 2755 2756 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) 2757 { 2758 if (eap->cmdidx == CMD_saveas && alt_buf != NULL) 2759 { 2760 #ifdef FEAT_AUTOCMD 2761 buf_T *was_curbuf = curbuf; 2762 2763 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 2764 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); 2765 # ifdef FEAT_EVAL 2766 if (curbuf != was_curbuf || aborting()) 2767 # else 2768 if (curbuf != was_curbuf) 2769 # endif 2770 { 2771 /* buffer changed, don't change name now */ 2772 retval = FAIL; 2773 goto theend; 2774 } 2775 #endif 2776 /* Exchange the file names for the current and the alternate 2777 * buffer. This makes it look like we are now editing the buffer 2778 * under the new name. Must be done before buf_write(), because 2779 * if there is no file name and 'cpo' contains 'F', it will set 2780 * the file name. */ 2781 fname = alt_buf->b_fname; 2782 alt_buf->b_fname = curbuf->b_fname; 2783 curbuf->b_fname = fname; 2784 fname = alt_buf->b_ffname; 2785 alt_buf->b_ffname = curbuf->b_ffname; 2786 curbuf->b_ffname = fname; 2787 fname = alt_buf->b_sfname; 2788 alt_buf->b_sfname = curbuf->b_sfname; 2789 curbuf->b_sfname = fname; 2790 buf_name_changed(curbuf); 2791 #ifdef FEAT_AUTOCMD 2792 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 2793 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); 2794 if (!alt_buf->b_p_bl) 2795 { 2796 alt_buf->b_p_bl = TRUE; 2797 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); 2798 } 2799 # ifdef FEAT_EVAL 2800 if (curbuf != was_curbuf || aborting()) 2801 # else 2802 if (curbuf != was_curbuf) 2803 # endif 2804 { 2805 /* buffer changed, don't write the file */ 2806 retval = FAIL; 2807 goto theend; 2808 } 2809 2810 /* If 'filetype' was empty try detecting it now. */ 2811 if (*curbuf->b_p_ft == NUL) 2812 { 2813 if (au_has_group((char_u *)"filetypedetect")) 2814 (void)do_doautocmd((char_u *)"filetypedetect BufRead", 2815 TRUE); 2816 do_modelines(0); 2817 } 2818 2819 /* Autocommands may have changed buffer names, esp. when 2820 * 'autochdir' is set. */ 2821 fname = curbuf->b_sfname; 2822 #endif 2823 } 2824 2825 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, 2826 eap, eap->append, eap->forceit, TRUE, FALSE); 2827 2828 /* After ":saveas fname" reset 'readonly'. */ 2829 if (eap->cmdidx == CMD_saveas) 2830 { 2831 if (retval == OK) 2832 { 2833 curbuf->b_p_ro = FALSE; 2834 #ifdef FEAT_WINDOWS 2835 redraw_tabline = TRUE; 2836 #endif 2837 } 2838 /* Change directories when the 'acd' option is set. */ 2839 DO_AUTOCHDIR 2840 } 2841 } 2842 2843 theend: 2844 #ifdef FEAT_BROWSE 2845 vim_free(browse_file); 2846 #endif 2847 vim_free(free_fname); 2848 return retval; 2849 } 2850 2851 /* 2852 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, 2853 * BF_NEW or BF_READERR, check for overwriting current file. 2854 * May set eap->forceit if a dialog says it's OK to overwrite. 2855 * Return OK if it's OK, FAIL if it is not. 2856 */ 2857 int 2858 check_overwrite( 2859 exarg_T *eap, 2860 buf_T *buf, 2861 char_u *fname, /* file name to be used (can differ from 2862 buf->ffname) */ 2863 char_u *ffname, /* full path version of fname */ 2864 int other) /* writing under other name */ 2865 { 2866 /* 2867 * write to other file or b_flags set or not writing the whole file: 2868 * overwriting only allowed with '!' 2869 */ 2870 if ( (other 2871 || (buf->b_flags & BF_NOTEDITED) 2872 || ((buf->b_flags & BF_NEW) 2873 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) 2874 || (buf->b_flags & BF_READERR)) 2875 && !p_wa 2876 #ifdef FEAT_QUICKFIX 2877 && !bt_nofile(buf) 2878 #endif 2879 && vim_fexists(ffname)) 2880 { 2881 if (!eap->forceit && !eap->append) 2882 { 2883 #ifdef UNIX 2884 /* with UNIX it is possible to open a directory */ 2885 if (mch_isdir(ffname)) 2886 { 2887 EMSG2(_(e_isadir2), ffname); 2888 return FAIL; 2889 } 2890 #endif 2891 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2892 if (p_confirm || cmdmod.confirm) 2893 { 2894 char_u buff[DIALOG_MSG_SIZE]; 2895 2896 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); 2897 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) 2898 return FAIL; 2899 eap->forceit = TRUE; 2900 } 2901 else 2902 #endif 2903 { 2904 EMSG(_(e_exists)); 2905 return FAIL; 2906 } 2907 } 2908 2909 /* For ":w! filename" check that no swap file exists for "filename". */ 2910 if (other && !emsg_silent) 2911 { 2912 char_u *dir; 2913 char_u *p; 2914 int r; 2915 char_u *swapname; 2916 2917 /* We only try the first entry in 'directory', without checking if 2918 * it's writable. If the "." directory is not writable the write 2919 * will probably fail anyway. 2920 * Use 'shortname' of the current buffer, since there is no buffer 2921 * for the written file. */ 2922 if (*p_dir == NUL) 2923 { 2924 dir = alloc(5); 2925 if (dir == NULL) 2926 return FAIL; 2927 STRCPY(dir, "."); 2928 } 2929 else 2930 { 2931 dir = alloc(MAXPATHL); 2932 if (dir == NULL) 2933 return FAIL; 2934 p = p_dir; 2935 copy_option_part(&p, dir, MAXPATHL, ","); 2936 } 2937 swapname = makeswapname(fname, ffname, curbuf, dir); 2938 vim_free(dir); 2939 r = vim_fexists(swapname); 2940 if (r) 2941 { 2942 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2943 if (p_confirm || cmdmod.confirm) 2944 { 2945 char_u buff[DIALOG_MSG_SIZE]; 2946 2947 dialog_msg(buff, 2948 _("Swap file \"%s\" exists, overwrite anyway?"), 2949 swapname); 2950 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) 2951 != VIM_YES) 2952 { 2953 vim_free(swapname); 2954 return FAIL; 2955 } 2956 eap->forceit = TRUE; 2957 } 2958 else 2959 #endif 2960 { 2961 EMSG2(_("E768: Swap file exists: %s (:silent! overrides)"), 2962 swapname); 2963 vim_free(swapname); 2964 return FAIL; 2965 } 2966 } 2967 vim_free(swapname); 2968 } 2969 } 2970 return OK; 2971 } 2972 2973 /* 2974 * Handle ":wnext", ":wNext" and ":wprevious" commands. 2975 */ 2976 void 2977 ex_wnext(exarg_T *eap) 2978 { 2979 int i; 2980 2981 if (eap->cmd[1] == 'n') 2982 i = curwin->w_arg_idx + (int)eap->line2; 2983 else 2984 i = curwin->w_arg_idx - (int)eap->line2; 2985 eap->line1 = 1; 2986 eap->line2 = curbuf->b_ml.ml_line_count; 2987 if (do_write(eap) != FAIL) 2988 do_argfile(eap, i); 2989 } 2990 2991 /* 2992 * ":wall", ":wqall" and ":xall": Write all changed files (and exit). 2993 */ 2994 void 2995 do_wqall(exarg_T *eap) 2996 { 2997 buf_T *buf; 2998 int error = 0; 2999 int save_forceit = eap->forceit; 3000 3001 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) 3002 exiting = TRUE; 3003 3004 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 3005 { 3006 if (bufIsChanged(buf)) 3007 { 3008 /* 3009 * Check if there is a reason the buffer cannot be written: 3010 * 1. if the 'write' option is set 3011 * 2. if there is no file name (even after browsing) 3012 * 3. if the 'readonly' is set (even after a dialog) 3013 * 4. if overwriting is allowed (even after a dialog) 3014 */ 3015 if (not_writing()) 3016 { 3017 ++error; 3018 break; 3019 } 3020 #ifdef FEAT_BROWSE 3021 /* ":browse wall": ask for file name if there isn't one */ 3022 if (buf->b_ffname == NULL && cmdmod.browse) 3023 browse_save_fname(buf); 3024 #endif 3025 if (buf->b_ffname == NULL) 3026 { 3027 EMSGN(_("E141: No file name for buffer %ld"), (long)buf->b_fnum); 3028 ++error; 3029 } 3030 else if (check_readonly(&eap->forceit, buf) 3031 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, 3032 FALSE) == FAIL) 3033 { 3034 ++error; 3035 } 3036 else 3037 { 3038 if (buf_write_all(buf, eap->forceit) == FAIL) 3039 ++error; 3040 #ifdef FEAT_AUTOCMD 3041 /* an autocommand may have deleted the buffer */ 3042 if (!buf_valid(buf)) 3043 buf = firstbuf; 3044 #endif 3045 } 3046 eap->forceit = save_forceit; /* check_overwrite() may set it */ 3047 } 3048 } 3049 if (exiting) 3050 { 3051 if (!error) 3052 getout(0); /* exit Vim */ 3053 not_exiting(); 3054 } 3055 } 3056 3057 /* 3058 * Check the 'write' option. 3059 * Return TRUE and give a message when it's not st. 3060 */ 3061 int 3062 not_writing(void) 3063 { 3064 if (p_write) 3065 return FALSE; 3066 EMSG(_("E142: File not written: Writing is disabled by 'write' option")); 3067 return TRUE; 3068 } 3069 3070 /* 3071 * Check if a buffer is read-only (either 'readonly' option is set or file is 3072 * read-only). Ask for overruling in a dialog. Return TRUE and give an error 3073 * message when the buffer is readonly. 3074 */ 3075 static int 3076 check_readonly(int *forceit, buf_T *buf) 3077 { 3078 struct stat st; 3079 3080 /* Handle a file being readonly when the 'readonly' option is set or when 3081 * the file exists and permissions are read-only. 3082 * We will send 0777 to check_file_readonly(), as the "perm" variable is 3083 * important for device checks but not here. */ 3084 if (!*forceit && (buf->b_p_ro 3085 || (mch_stat((char *)buf->b_ffname, &st) >= 0 3086 && check_file_readonly(buf->b_ffname, 0777)))) 3087 { 3088 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 3089 if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) 3090 { 3091 char_u buff[DIALOG_MSG_SIZE]; 3092 3093 if (buf->b_p_ro) 3094 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), 3095 buf->b_fname); 3096 else 3097 dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), 3098 buf->b_fname); 3099 3100 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) 3101 { 3102 /* Set forceit, to force the writing of a readonly file */ 3103 *forceit = TRUE; 3104 return FALSE; 3105 } 3106 else 3107 return TRUE; 3108 } 3109 else 3110 #endif 3111 if (buf->b_p_ro) 3112 EMSG(_(e_readonly)); 3113 else 3114 EMSG2(_("E505: \"%s\" is read-only (add ! to override)"), 3115 buf->b_fname); 3116 return TRUE; 3117 } 3118 3119 return FALSE; 3120 } 3121 3122 /* 3123 * Try to abandon current file and edit a new or existing file. 3124 * 'fnum' is the number of the file, if zero use ffname/sfname. 3125 * 3126 * Return 1 for "normal" error, 2 for "not written" error, 0 for success 3127 * -1 for successfully opening another file. 3128 * 'lnum' is the line number for the cursor in the new file (if non-zero). 3129 */ 3130 int 3131 getfile( 3132 int fnum, 3133 char_u *ffname, 3134 char_u *sfname, 3135 int setpm, 3136 linenr_T lnum, 3137 int forceit) 3138 { 3139 int other; 3140 int retval; 3141 char_u *free_me = NULL; 3142 3143 if (text_locked()) 3144 return 1; 3145 #ifdef FEAT_AUTOCMD 3146 if (curbuf_locked()) 3147 return 1; 3148 #endif 3149 3150 if (fnum == 0) 3151 { 3152 /* make ffname full path, set sfname */ 3153 fname_expand(curbuf, &ffname, &sfname); 3154 other = otherfile(ffname); 3155 free_me = ffname; /* has been allocated, free() later */ 3156 } 3157 else 3158 other = (fnum != curbuf->b_fnum); 3159 3160 if (other) 3161 ++no_wait_return; /* don't wait for autowrite message */ 3162 if (other && !forceit && curbuf->b_nwindows == 1 && !P_HID(curbuf) 3163 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) 3164 { 3165 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 3166 if (p_confirm && p_write) 3167 dialog_changed(curbuf, FALSE); 3168 if (curbufIsChanged()) 3169 #endif 3170 { 3171 if (other) 3172 --no_wait_return; 3173 EMSG(_(e_nowrtmsg)); 3174 retval = 2; /* file has been changed */ 3175 goto theend; 3176 } 3177 } 3178 if (other) 3179 --no_wait_return; 3180 if (setpm) 3181 setpcmark(); 3182 if (!other) 3183 { 3184 if (lnum != 0) 3185 curwin->w_cursor.lnum = lnum; 3186 check_cursor_lnum(); 3187 beginline(BL_SOL | BL_FIX); 3188 retval = 0; /* it's in the same file */ 3189 } 3190 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, 3191 (P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0), 3192 curwin) == OK) 3193 retval = -1; /* opened another file */ 3194 else 3195 retval = 1; /* error encountered */ 3196 3197 theend: 3198 vim_free(free_me); 3199 return retval; 3200 } 3201 3202 /* 3203 * start editing a new file 3204 * 3205 * fnum: file number; if zero use ffname/sfname 3206 * ffname: the file name 3207 * - full path if sfname used, 3208 * - any file name if sfname is NULL 3209 * - empty string to re-edit with the same file name (but may be 3210 * in a different directory) 3211 * - NULL to start an empty buffer 3212 * sfname: the short file name (or NULL) 3213 * eap: contains the command to be executed after loading the file and 3214 * forced 'ff' and 'fenc' 3215 * newlnum: if > 0: put cursor on this line number (if possible) 3216 * if ECMD_LASTL: use last position in loaded file 3217 * if ECMD_LAST: use last position in all files 3218 * if ECMD_ONE: use first line 3219 * flags: 3220 * ECMD_HIDE: if TRUE don't free the current buffer 3221 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file 3222 * ECMD_OLDBUF: use existing buffer if it exists 3223 * ECMD_FORCEIT: ! used for Ex command 3224 * ECMD_ADDBUF: don't edit, just add to buffer list 3225 * oldwin: Should be "curwin" when editing a new buffer in the current 3226 * window, NULL when splitting the window first. When not NULL info 3227 * of the previous buffer for "oldwin" is stored. 3228 * 3229 * return FAIL for failure, OK otherwise 3230 */ 3231 int 3232 do_ecmd( 3233 int fnum, 3234 char_u *ffname, 3235 char_u *sfname, 3236 exarg_T *eap, /* can be NULL! */ 3237 linenr_T newlnum, 3238 int flags, 3239 win_T *oldwin) 3240 { 3241 int other_file; /* TRUE if editing another file */ 3242 int oldbuf; /* TRUE if using existing buffer */ 3243 #ifdef FEAT_AUTOCMD 3244 int auto_buf = FALSE; /* TRUE if autocommands brought us 3245 into the buffer unexpectedly */ 3246 char_u *new_name = NULL; 3247 int did_set_swapcommand = FALSE; 3248 #endif 3249 buf_T *buf; 3250 #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 3251 buf_T *old_curbuf = curbuf; 3252 #endif 3253 char_u *free_fname = NULL; 3254 #ifdef FEAT_BROWSE 3255 char_u *browse_file = NULL; 3256 #endif 3257 int retval = FAIL; 3258 long n; 3259 pos_T orig_pos; 3260 linenr_T topline = 0; 3261 int newcol = -1; 3262 int solcol = -1; 3263 pos_T *pos; 3264 #ifdef FEAT_SUN_WORKSHOP 3265 char_u *cp; 3266 #endif 3267 char_u *command = NULL; 3268 #ifdef FEAT_SPELL 3269 int did_get_winopts = FALSE; 3270 #endif 3271 int readfile_flags = 0; 3272 int did_inc_redrawing_disabled = FALSE; 3273 3274 if (eap != NULL) 3275 command = eap->do_ecmd_cmd; 3276 3277 if (fnum != 0) 3278 { 3279 if (fnum == curbuf->b_fnum) /* file is already being edited */ 3280 return OK; /* nothing to do */ 3281 other_file = TRUE; 3282 } 3283 else 3284 { 3285 #ifdef FEAT_BROWSE 3286 if (cmdmod.browse) 3287 { 3288 # ifdef FEAT_AUTOCMD 3289 if ( 3290 # ifdef FEAT_GUI 3291 !gui.in_use && 3292 # endif 3293 au_has_group((char_u *)"FileExplorer")) 3294 { 3295 /* No browsing supported but we do have the file explorer: 3296 * Edit the directory. */ 3297 if (ffname == NULL || !mch_isdir(ffname)) 3298 ffname = (char_u *)"."; 3299 } 3300 else 3301 # endif 3302 { 3303 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, 3304 NULL, NULL, NULL, curbuf); 3305 if (browse_file == NULL) 3306 goto theend; 3307 ffname = browse_file; 3308 } 3309 } 3310 #endif 3311 /* if no short name given, use ffname for short name */ 3312 if (sfname == NULL) 3313 sfname = ffname; 3314 #ifdef USE_FNAME_CASE 3315 # ifdef USE_LONG_FNAME 3316 if (USE_LONG_FNAME) 3317 # endif 3318 if (sfname != NULL) 3319 fname_case(sfname, 0); /* set correct case for sfname */ 3320 #endif 3321 3322 #ifdef FEAT_LISTCMDS 3323 if ((flags & ECMD_ADDBUF) && (ffname == NULL || *ffname == NUL)) 3324 goto theend; 3325 #endif 3326 3327 if (ffname == NULL) 3328 other_file = TRUE; 3329 /* there is no file name */ 3330 else if (*ffname == NUL && curbuf->b_ffname == NULL) 3331 other_file = FALSE; 3332 else 3333 { 3334 if (*ffname == NUL) /* re-edit with same file name */ 3335 { 3336 ffname = curbuf->b_ffname; 3337 sfname = curbuf->b_fname; 3338 } 3339 free_fname = fix_fname(ffname); /* may expand to full path name */ 3340 if (free_fname != NULL) 3341 ffname = free_fname; 3342 other_file = otherfile(ffname); 3343 #ifdef FEAT_SUN_WORKSHOP 3344 if (usingSunWorkShop && p_acd 3345 && (cp = vim_strrchr(sfname, '/')) != NULL) 3346 sfname = ++cp; 3347 #endif 3348 } 3349 } 3350 3351 /* 3352 * if the file was changed we may not be allowed to abandon it 3353 * - if we are going to re-edit the same file 3354 * - or if we are the only window on this file and if ECMD_HIDE is FALSE 3355 */ 3356 if ( ((!other_file && !(flags & ECMD_OLDBUF)) 3357 || (curbuf->b_nwindows == 1 3358 && !(flags & (ECMD_HIDE | ECMD_ADDBUF)))) 3359 && check_changed(curbuf, (p_awa ? CCGD_AW : 0) 3360 | (other_file ? 0 : CCGD_MULTWIN) 3361 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0) 3362 | (eap == NULL ? 0 : CCGD_EXCMD))) 3363 { 3364 if (fnum == 0 && other_file && ffname != NULL) 3365 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); 3366 goto theend; 3367 } 3368 3369 /* 3370 * End Visual mode before switching to another buffer, so the text can be 3371 * copied into the GUI selection buffer. 3372 */ 3373 reset_VIsual(); 3374 3375 #ifdef FEAT_AUTOCMD 3376 if ((command != NULL || newlnum > (linenr_T)0) 3377 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) 3378 { 3379 int len; 3380 char_u *p; 3381 3382 /* Set v:swapcommand for the SwapExists autocommands. */ 3383 if (command != NULL) 3384 len = (int)STRLEN(command) + 3; 3385 else 3386 len = 30; 3387 p = alloc((unsigned)len); 3388 if (p != NULL) 3389 { 3390 if (command != NULL) 3391 vim_snprintf((char *)p, len, ":%s\r", command); 3392 else 3393 vim_snprintf((char *)p, len, "%ldG", (long)newlnum); 3394 set_vim_var_string(VV_SWAPCOMMAND, p, -1); 3395 did_set_swapcommand = TRUE; 3396 vim_free(p); 3397 } 3398 } 3399 #endif 3400 3401 /* 3402 * If we are starting to edit another file, open a (new) buffer. 3403 * Otherwise we re-use the current buffer. 3404 */ 3405 if (other_file) 3406 { 3407 #ifdef FEAT_LISTCMDS 3408 if (!(flags & ECMD_ADDBUF)) 3409 #endif 3410 { 3411 if (!cmdmod.keepalt) 3412 curwin->w_alt_fnum = curbuf->b_fnum; 3413 if (oldwin != NULL) 3414 buflist_altfpos(oldwin); 3415 } 3416 3417 if (fnum) 3418 buf = buflist_findnr(fnum); 3419 else 3420 { 3421 #ifdef FEAT_LISTCMDS 3422 if (flags & ECMD_ADDBUF) 3423 { 3424 linenr_T tlnum = 1L; 3425 3426 if (command != NULL) 3427 { 3428 tlnum = atol((char *)command); 3429 if (tlnum <= 0) 3430 tlnum = 1L; 3431 } 3432 (void)buflist_new(ffname, sfname, tlnum, BLN_LISTED); 3433 goto theend; 3434 } 3435 #endif 3436 buf = buflist_new(ffname, sfname, 0L, 3437 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); 3438 #ifdef FEAT_AUTOCMD 3439 /* autocommands may change curwin and curbuf */ 3440 if (oldwin != NULL) 3441 oldwin = curwin; 3442 old_curbuf = curbuf; 3443 #endif 3444 } 3445 if (buf == NULL) 3446 goto theend; 3447 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ 3448 { 3449 oldbuf = FALSE; 3450 } 3451 else /* existing memfile */ 3452 { 3453 oldbuf = TRUE; 3454 (void)buf_check_timestamp(buf, FALSE); 3455 /* Check if autocommands made buffer invalid or changed the current 3456 * buffer. */ 3457 if (!buf_valid(buf) 3458 #ifdef FEAT_AUTOCMD 3459 || curbuf != old_curbuf 3460 #endif 3461 ) 3462 goto theend; 3463 #ifdef FEAT_EVAL 3464 if (aborting()) /* autocmds may abort script processing */ 3465 goto theend; 3466 #endif 3467 } 3468 3469 /* May jump to last used line number for a loaded buffer or when asked 3470 * for explicitly */ 3471 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) 3472 { 3473 pos = buflist_findfpos(buf); 3474 newlnum = pos->lnum; 3475 solcol = pos->col; 3476 } 3477 3478 /* 3479 * Make the (new) buffer the one used by the current window. 3480 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. 3481 * If the current buffer was empty and has no file name, curbuf 3482 * is returned by buflist_new(), nothing to do here. 3483 */ 3484 if (buf != curbuf) 3485 { 3486 #ifdef FEAT_AUTOCMD 3487 /* 3488 * Be careful: The autocommands may delete any buffer and change 3489 * the current buffer. 3490 * - If the buffer we are going to edit is deleted, give up. 3491 * - If the current buffer is deleted, prefer to load the new 3492 * buffer when loading a buffer is required. This avoids 3493 * loading another buffer which then must be closed again. 3494 * - If we ended up in the new buffer already, need to skip a few 3495 * things, set auto_buf. 3496 */ 3497 if (buf->b_fname != NULL) 3498 new_name = vim_strsave(buf->b_fname); 3499 au_new_curbuf = buf; 3500 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 3501 if (!buf_valid(buf)) /* new buffer has been deleted */ 3502 { 3503 delbuf_msg(new_name); /* frees new_name */ 3504 goto theend; 3505 } 3506 # ifdef FEAT_EVAL 3507 if (aborting()) /* autocmds may abort script processing */ 3508 { 3509 vim_free(new_name); 3510 goto theend; 3511 } 3512 # endif 3513 if (buf == curbuf) /* already in new buffer */ 3514 auto_buf = TRUE; 3515 else 3516 { 3517 if (curbuf == old_curbuf) 3518 #endif 3519 buf_copy_options(buf, BCO_ENTER); 3520 3521 /* close the link to the current buffer */ 3522 u_sync(FALSE); 3523 close_buffer(oldwin, curbuf, 3524 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE); 3525 3526 #ifdef FEAT_AUTOCMD 3527 /* Autocommands may open a new window and leave oldwin open 3528 * which leads to crashes since the above call sets 3529 * oldwin->w_buffer to NULL. */ 3530 if (curwin != oldwin && oldwin != aucmd_win 3531 && win_valid(oldwin) && oldwin->w_buffer == NULL) 3532 win_close(oldwin, FALSE); 3533 3534 # ifdef FEAT_EVAL 3535 if (aborting()) /* autocmds may abort script processing */ 3536 { 3537 vim_free(new_name); 3538 goto theend; 3539 } 3540 # endif 3541 /* Be careful again, like above. */ 3542 if (!buf_valid(buf)) /* new buffer has been deleted */ 3543 { 3544 delbuf_msg(new_name); /* frees new_name */ 3545 goto theend; 3546 } 3547 if (buf == curbuf) /* already in new buffer */ 3548 auto_buf = TRUE; 3549 else 3550 #endif 3551 { 3552 #ifdef FEAT_SYN_HL 3553 /* 3554 * <VN> We could instead free the synblock 3555 * and re-attach to buffer, perhaps. 3556 */ 3557 if (curwin->w_s == &(curwin->w_buffer->b_s)) 3558 curwin->w_s = &(buf->b_s); 3559 #endif 3560 curwin->w_buffer = buf; 3561 curbuf = buf; 3562 ++curbuf->b_nwindows; 3563 3564 /* Set 'fileformat', 'binary' and 'fenc' when forced. */ 3565 if (!oldbuf && eap != NULL) 3566 { 3567 set_file_options(TRUE, eap); 3568 #ifdef FEAT_MBYTE 3569 set_forced_fenc(eap); 3570 #endif 3571 } 3572 } 3573 3574 /* May get the window options from the last time this buffer 3575 * was in this window (or another window). If not used 3576 * before, reset the local window options to the global 3577 * values. Also restores old folding stuff. */ 3578 get_winopts(curbuf); 3579 #ifdef FEAT_SPELL 3580 did_get_winopts = TRUE; 3581 #endif 3582 3583 #ifdef FEAT_AUTOCMD 3584 } 3585 vim_free(new_name); 3586 au_new_curbuf = NULL; 3587 #endif 3588 } 3589 3590 curwin->w_pcmark.lnum = 1; 3591 curwin->w_pcmark.col = 0; 3592 } 3593 else /* !other_file */ 3594 { 3595 if ( 3596 #ifdef FEAT_LISTCMDS 3597 (flags & ECMD_ADDBUF) || 3598 #endif 3599 check_fname() == FAIL) 3600 goto theend; 3601 3602 oldbuf = (flags & ECMD_OLDBUF); 3603 } 3604 3605 /* Don't redraw until the cursor is in the right line, otherwise 3606 * autocommands may cause ml_get errors. */ 3607 ++RedrawingDisabled; 3608 did_inc_redrawing_disabled = TRUE; 3609 3610 #ifdef FEAT_AUTOCMD 3611 buf = curbuf; 3612 #endif 3613 if ((flags & ECMD_SET_HELP) || keep_help_flag) 3614 { 3615 prepare_help_buffer(); 3616 } 3617 else 3618 { 3619 /* Don't make a buffer listed if it's a help buffer. Useful when 3620 * using CTRL-O to go back to a help file. */ 3621 if (!curbuf->b_help) 3622 set_buflisted(TRUE); 3623 } 3624 3625 #ifdef FEAT_AUTOCMD 3626 /* If autocommands change buffers under our fingers, forget about 3627 * editing the file. */ 3628 if (buf != curbuf) 3629 goto theend; 3630 # ifdef FEAT_EVAL 3631 if (aborting()) /* autocmds may abort script processing */ 3632 goto theend; 3633 # endif 3634 3635 /* Since we are starting to edit a file, consider the filetype to be 3636 * unset. Helps for when an autocommand changes files and expects syntax 3637 * highlighting to work in the other file. */ 3638 did_filetype = FALSE; 3639 #endif 3640 3641 /* 3642 * other_file oldbuf 3643 * FALSE FALSE re-edit same file, buffer is re-used 3644 * FALSE TRUE re-edit same file, nothing changes 3645 * TRUE FALSE start editing new file, new buffer 3646 * TRUE TRUE start editing in existing buffer (nothing to do) 3647 */ 3648 if (!other_file && !oldbuf) /* re-use the buffer */ 3649 { 3650 set_last_cursor(curwin); /* may set b_last_cursor */ 3651 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) 3652 { 3653 newlnum = curwin->w_cursor.lnum; 3654 solcol = curwin->w_cursor.col; 3655 } 3656 #ifdef FEAT_AUTOCMD 3657 buf = curbuf; 3658 if (buf->b_fname != NULL) 3659 new_name = vim_strsave(buf->b_fname); 3660 else 3661 new_name = NULL; 3662 #endif 3663 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) 3664 { 3665 /* Save all the text, so that the reload can be undone. 3666 * Sync first so that this is a separate undo-able action. */ 3667 u_sync(FALSE); 3668 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) 3669 == FAIL) 3670 goto theend; 3671 u_unchanged(curbuf); 3672 buf_freeall(curbuf, BFA_KEEP_UNDO); 3673 3674 /* tell readfile() not to clear or reload undo info */ 3675 readfile_flags = READ_KEEP_UNDO; 3676 } 3677 else 3678 buf_freeall(curbuf, 0); /* free all things for buffer */ 3679 #ifdef FEAT_AUTOCMD 3680 /* If autocommands deleted the buffer we were going to re-edit, give 3681 * up and jump to the end. */ 3682 if (!buf_valid(buf)) 3683 { 3684 delbuf_msg(new_name); /* frees new_name */ 3685 goto theend; 3686 } 3687 vim_free(new_name); 3688 3689 /* If autocommands change buffers under our fingers, forget about 3690 * re-editing the file. Should do the buf_clear_file(), but perhaps 3691 * the autocommands changed the buffer... */ 3692 if (buf != curbuf) 3693 goto theend; 3694 # ifdef FEAT_EVAL 3695 if (aborting()) /* autocmds may abort script processing */ 3696 goto theend; 3697 # endif 3698 #endif 3699 buf_clear_file(curbuf); 3700 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ 3701 curbuf->b_op_end.lnum = 0; 3702 } 3703 3704 /* 3705 * If we get here we are sure to start editing 3706 */ 3707 /* Assume success now */ 3708 retval = OK; 3709 3710 /* 3711 * Reset cursor position, could be used by autocommands. 3712 */ 3713 check_cursor(); 3714 3715 /* 3716 * Check if we are editing the w_arg_idx file in the argument list. 3717 */ 3718 check_arg_idx(curwin); 3719 3720 #ifdef FEAT_AUTOCMD 3721 if (!auto_buf) 3722 #endif 3723 { 3724 /* 3725 * Set cursor and init window before reading the file and executing 3726 * autocommands. This allows for the autocommands to position the 3727 * cursor. 3728 */ 3729 curwin_init(); 3730 3731 #ifdef FEAT_FOLDING 3732 /* It's possible that all lines in the buffer changed. Need to update 3733 * automatic folding for all windows where it's used. */ 3734 # ifdef FEAT_WINDOWS 3735 { 3736 win_T *win; 3737 tabpage_T *tp; 3738 3739 FOR_ALL_TAB_WINDOWS(tp, win) 3740 if (win->w_buffer == curbuf) 3741 foldUpdateAll(win); 3742 } 3743 # else 3744 foldUpdateAll(curwin); 3745 # endif 3746 #endif 3747 3748 /* Change directories when the 'acd' option is set. */ 3749 DO_AUTOCHDIR 3750 3751 /* 3752 * Careful: open_buffer() and apply_autocmds() may change the current 3753 * buffer and window. 3754 */ 3755 orig_pos = curwin->w_cursor; 3756 topline = curwin->w_topline; 3757 if (!oldbuf) /* need to read the file */ 3758 { 3759 #if defined(HAS_SWAP_EXISTS_ACTION) 3760 swap_exists_action = SEA_DIALOG; 3761 #endif 3762 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ 3763 3764 /* 3765 * Open the buffer and read the file. 3766 */ 3767 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 3768 if (should_abort(open_buffer(FALSE, eap, readfile_flags))) 3769 retval = FAIL; 3770 #else 3771 (void)open_buffer(FALSE, eap, readfile_flags); 3772 #endif 3773 3774 #if defined(HAS_SWAP_EXISTS_ACTION) 3775 if (swap_exists_action == SEA_QUIT) 3776 retval = FAIL; 3777 handle_swap_exists(old_curbuf); 3778 #endif 3779 } 3780 #ifdef FEAT_AUTOCMD 3781 else 3782 { 3783 /* Read the modelines, but only to set window-local options. Any 3784 * buffer-local options have already been set and may have been 3785 * changed by the user. */ 3786 do_modelines(OPT_WINONLY); 3787 3788 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, 3789 &retval); 3790 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, 3791 &retval); 3792 } 3793 check_arg_idx(curwin); 3794 #endif 3795 3796 /* If autocommands change the cursor position or topline, we should 3797 * keep it. Also when it moves within a line. */ 3798 if (!equalpos(curwin->w_cursor, orig_pos)) 3799 { 3800 newlnum = curwin->w_cursor.lnum; 3801 newcol = curwin->w_cursor.col; 3802 } 3803 if (curwin->w_topline == topline) 3804 topline = 0; 3805 3806 /* Even when cursor didn't move we need to recompute topline. */ 3807 changed_line_abv_curs(); 3808 3809 #ifdef FEAT_TITLE 3810 maketitle(); 3811 #endif 3812 } 3813 3814 #ifdef FEAT_DIFF 3815 /* Tell the diff stuff that this buffer is new and/or needs updating. 3816 * Also needed when re-editing the same buffer, because unloading will 3817 * have removed it as a diff buffer. */ 3818 if (curwin->w_p_diff) 3819 { 3820 diff_buf_add(curbuf); 3821 diff_invalidate(curbuf); 3822 } 3823 #endif 3824 3825 #ifdef FEAT_SPELL 3826 /* If the window options were changed may need to set the spell language. 3827 * Can only do this after the buffer has been properly setup. */ 3828 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) 3829 (void)did_set_spelllang(curwin); 3830 #endif 3831 3832 if (command == NULL) 3833 { 3834 if (newcol >= 0) /* position set by autocommands */ 3835 { 3836 curwin->w_cursor.lnum = newlnum; 3837 curwin->w_cursor.col = newcol; 3838 check_cursor(); 3839 } 3840 else if (newlnum > 0) /* line number from caller or old position */ 3841 { 3842 curwin->w_cursor.lnum = newlnum; 3843 check_cursor_lnum(); 3844 if (solcol >= 0 && !p_sol) 3845 { 3846 /* 'sol' is off: Use last known column. */ 3847 curwin->w_cursor.col = solcol; 3848 check_cursor_col(); 3849 #ifdef FEAT_VIRTUALEDIT 3850 curwin->w_cursor.coladd = 0; 3851 #endif 3852 curwin->w_set_curswant = TRUE; 3853 } 3854 else 3855 beginline(BL_SOL | BL_FIX); 3856 } 3857 else /* no line number, go to last line in Ex mode */ 3858 { 3859 if (exmode_active) 3860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 3861 beginline(BL_WHITE | BL_FIX); 3862 } 3863 } 3864 3865 #ifdef FEAT_WINDOWS 3866 /* Check if cursors in other windows on the same buffer are still valid */ 3867 check_lnums(FALSE); 3868 #endif 3869 3870 /* 3871 * Did not read the file, need to show some info about the file. 3872 * Do this after setting the cursor. 3873 */ 3874 if (oldbuf 3875 #ifdef FEAT_AUTOCMD 3876 && !auto_buf 3877 #endif 3878 ) 3879 { 3880 int msg_scroll_save = msg_scroll; 3881 3882 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file 3883 * message. */ 3884 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) 3885 msg_scroll = FALSE; 3886 if (!msg_scroll) /* wait a bit when overwriting an error msg */ 3887 check_for_delay(FALSE); 3888 msg_start(); 3889 msg_scroll = msg_scroll_save; 3890 msg_scrolled_ign = TRUE; 3891 3892 if (!shortmess(SHM_FILEINFO)) 3893 fileinfo(FALSE, TRUE, FALSE); 3894 3895 msg_scrolled_ign = FALSE; 3896 } 3897 3898 if (command != NULL) 3899 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE); 3900 3901 #ifdef FEAT_KEYMAP 3902 if (curbuf->b_kmap_state & KEYMAP_INIT) 3903 (void)keymap_init(); 3904 #endif 3905 3906 --RedrawingDisabled; 3907 did_inc_redrawing_disabled = FALSE; 3908 if (!skip_redraw) 3909 { 3910 n = p_so; 3911 if (topline == 0 && command == NULL) 3912 p_so = 999; /* force cursor halfway the window */ 3913 update_topline(); 3914 #ifdef FEAT_SCROLLBIND 3915 curwin->w_scbind_pos = curwin->w_topline; 3916 #endif 3917 p_so = n; 3918 redraw_curbuf_later(NOT_VALID); /* redraw this buffer later */ 3919 } 3920 3921 if (p_im) 3922 need_start_insertmode = TRUE; 3923 3924 /* Change directories when the 'acd' option is set. */ 3925 DO_AUTOCHDIR 3926 3927 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) 3928 if (curbuf->b_ffname != NULL) 3929 { 3930 # ifdef FEAT_SUN_WORKSHOP 3931 if (gui.in_use && usingSunWorkShop) 3932 workshop_file_opened((char *)curbuf->b_ffname, curbuf->b_p_ro); 3933 # endif 3934 # ifdef FEAT_NETBEANS_INTG 3935 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP) 3936 netbeans_file_opened(curbuf); 3937 # endif 3938 } 3939 #endif 3940 3941 theend: 3942 if (did_inc_redrawing_disabled) 3943 --RedrawingDisabled; 3944 #ifdef FEAT_AUTOCMD 3945 if (did_set_swapcommand) 3946 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); 3947 #endif 3948 #ifdef FEAT_BROWSE 3949 vim_free(browse_file); 3950 #endif 3951 vim_free(free_fname); 3952 return retval; 3953 } 3954 3955 #ifdef FEAT_AUTOCMD 3956 static void 3957 delbuf_msg(char_u *name) 3958 { 3959 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), 3960 name == NULL ? (char_u *)"" : name); 3961 vim_free(name); 3962 au_new_curbuf = NULL; 3963 } 3964 #endif 3965 3966 static int append_indent = 0; /* autoindent for first line */ 3967 3968 /* 3969 * ":insert" and ":append", also used by ":change" 3970 */ 3971 void 3972 ex_append(exarg_T *eap) 3973 { 3974 char_u *theline; 3975 int did_undo = FALSE; 3976 linenr_T lnum = eap->line2; 3977 int indent = 0; 3978 char_u *p; 3979 int vcol; 3980 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); 3981 3982 /* the ! flag toggles autoindent */ 3983 if (eap->forceit) 3984 curbuf->b_p_ai = !curbuf->b_p_ai; 3985 3986 /* First autoindent comes from the line we start on */ 3987 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) 3988 append_indent = get_indent_lnum(lnum); 3989 3990 if (eap->cmdidx != CMD_append) 3991 --lnum; 3992 3993 /* when the buffer is empty append to line 0 and delete the dummy line */ 3994 if (empty && lnum == 1) 3995 lnum = 0; 3996 3997 State = INSERT; /* behave like in Insert mode */ 3998 if (curbuf->b_p_iminsert == B_IMODE_LMAP) 3999 State |= LANGMAP; 4000 4001 for (;;) 4002 { 4003 msg_scroll = TRUE; 4004 need_wait_return = FALSE; 4005 if (curbuf->b_p_ai) 4006 { 4007 if (append_indent >= 0) 4008 { 4009 indent = append_indent; 4010 append_indent = -1; 4011 } 4012 else if (lnum > 0) 4013 indent = get_indent_lnum(lnum); 4014 } 4015 ex_keep_indent = FALSE; 4016 if (eap->getline == NULL) 4017 { 4018 /* No getline() function, use the lines that follow. This ends 4019 * when there is no more. */ 4020 if (eap->nextcmd == NULL || *eap->nextcmd == NUL) 4021 break; 4022 p = vim_strchr(eap->nextcmd, NL); 4023 if (p == NULL) 4024 p = eap->nextcmd + STRLEN(eap->nextcmd); 4025 theline = vim_strnsave(eap->nextcmd, (int)(p - eap->nextcmd)); 4026 if (*p != NUL) 4027 ++p; 4028 eap->nextcmd = p; 4029 } 4030 else 4031 { 4032 int save_State = State; 4033 4034 /* Set State to avoid the cursor shape to be set to INSERT mode 4035 * when getline() returns. */ 4036 State = CMDLINE; 4037 theline = eap->getline( 4038 #ifdef FEAT_EVAL 4039 eap->cstack->cs_looplevel > 0 ? -1 : 4040 #endif 4041 NUL, eap->cookie, indent); 4042 State = save_State; 4043 } 4044 lines_left = Rows - 1; 4045 if (theline == NULL) 4046 break; 4047 4048 /* Using ^ CTRL-D in getexmodeline() makes us repeat the indent. */ 4049 if (ex_keep_indent) 4050 append_indent = indent; 4051 4052 /* Look for the "." after automatic indent. */ 4053 vcol = 0; 4054 for (p = theline; indent > vcol; ++p) 4055 { 4056 if (*p == ' ') 4057 ++vcol; 4058 else if (*p == TAB) 4059 vcol += 8 - vcol % 8; 4060 else 4061 break; 4062 } 4063 if ((p[0] == '.' && p[1] == NUL) 4064 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) 4065 == FAIL)) 4066 { 4067 vim_free(theline); 4068 break; 4069 } 4070 4071 /* don't use autoindent if nothing was typed. */ 4072 if (p[0] == NUL) 4073 theline[0] = NUL; 4074 4075 did_undo = TRUE; 4076 ml_append(lnum, theline, (colnr_T)0, FALSE); 4077 appended_lines_mark(lnum, 1L); 4078 4079 vim_free(theline); 4080 ++lnum; 4081 4082 if (empty) 4083 { 4084 ml_delete(2L, FALSE); 4085 empty = FALSE; 4086 } 4087 } 4088 State = NORMAL; 4089 4090 if (eap->forceit) 4091 curbuf->b_p_ai = !curbuf->b_p_ai; 4092 4093 /* "start" is set to eap->line2+1 unless that position is invalid (when 4094 * eap->line2 pointed to the end of the buffer and nothing was appended) 4095 * "end" is set to lnum when something has been appended, otherwise 4096 * it is the same than "start" -- Acevedo */ 4097 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? 4098 eap->line2 + 1 : curbuf->b_ml.ml_line_count; 4099 if (eap->cmdidx != CMD_append) 4100 --curbuf->b_op_start.lnum; 4101 curbuf->b_op_end.lnum = (eap->line2 < lnum) 4102 ? lnum : curbuf->b_op_start.lnum; 4103 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 4104 curwin->w_cursor.lnum = lnum; 4105 check_cursor_lnum(); 4106 beginline(BL_SOL | BL_FIX); 4107 4108 need_wait_return = FALSE; /* don't use wait_return() now */ 4109 ex_no_reprint = TRUE; 4110 } 4111 4112 /* 4113 * ":change" 4114 */ 4115 void 4116 ex_change(exarg_T *eap) 4117 { 4118 linenr_T lnum; 4119 4120 if (eap->line2 >= eap->line1 4121 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) 4122 return; 4123 4124 /* the ! flag toggles autoindent */ 4125 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) 4126 append_indent = get_indent_lnum(eap->line1); 4127 4128 for (lnum = eap->line2; lnum >= eap->line1; --lnum) 4129 { 4130 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */ 4131 break; 4132 ml_delete(eap->line1, FALSE); 4133 } 4134 4135 /* make sure the cursor is not beyond the end of the file now */ 4136 check_cursor_lnum(); 4137 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); 4138 4139 /* ":append" on the line above the deleted lines. */ 4140 eap->line2 = eap->line1; 4141 ex_append(eap); 4142 } 4143 4144 void 4145 ex_z(exarg_T *eap) 4146 { 4147 char_u *x; 4148 int bigness; 4149 char_u *kind; 4150 int minus = 0; 4151 linenr_T start, end, curs, i; 4152 int j; 4153 linenr_T lnum = eap->line2; 4154 4155 /* Vi compatible: ":z!" uses display height, without a count uses 4156 * 'scroll' */ 4157 if (eap->forceit) 4158 bigness = curwin->w_height; 4159 #ifdef FEAT_WINDOWS 4160 else if (firstwin != lastwin) 4161 bigness = curwin->w_height - 3; 4162 #endif 4163 else 4164 bigness = curwin->w_p_scr * 2; 4165 if (bigness < 1) 4166 bigness = 1; 4167 4168 x = eap->arg; 4169 kind = x; 4170 if (*kind == '-' || *kind == '+' || *kind == '=' 4171 || *kind == '^' || *kind == '.') 4172 ++x; 4173 while (*x == '-' || *x == '+') 4174 ++x; 4175 4176 if (*x != 0) 4177 { 4178 if (!VIM_ISDIGIT(*x)) 4179 { 4180 EMSG(_("E144: non-numeric argument to :z")); 4181 return; 4182 } 4183 else 4184 { 4185 bigness = atoi((char *)x); 4186 p_window = bigness; 4187 if (*kind == '=') 4188 bigness += 2; 4189 } 4190 } 4191 4192 /* the number of '-' and '+' multiplies the distance */ 4193 if (*kind == '-' || *kind == '+') 4194 for (x = kind + 1; *x == *kind; ++x) 4195 ; 4196 4197 switch (*kind) 4198 { 4199 case '-': 4200 start = lnum - bigness * (linenr_T)(x - kind) + 1; 4201 end = start + bigness - 1; 4202 curs = end; 4203 break; 4204 4205 case '=': 4206 start = lnum - (bigness + 1) / 2 + 1; 4207 end = lnum + (bigness + 1) / 2 - 1; 4208 curs = lnum; 4209 minus = 1; 4210 break; 4211 4212 case '^': 4213 start = lnum - bigness * 2; 4214 end = lnum - bigness; 4215 curs = lnum - bigness; 4216 break; 4217 4218 case '.': 4219 start = lnum - (bigness + 1) / 2 + 1; 4220 end = lnum + (bigness + 1) / 2 - 1; 4221 curs = end; 4222 break; 4223 4224 default: /* '+' */ 4225 start = lnum; 4226 if (*kind == '+') 4227 start += bigness * (linenr_T)(x - kind - 1) + 1; 4228 else if (eap->addr_count == 0) 4229 ++start; 4230 end = start + bigness - 1; 4231 curs = end; 4232 break; 4233 } 4234 4235 if (start < 1) 4236 start = 1; 4237 4238 if (end > curbuf->b_ml.ml_line_count) 4239 end = curbuf->b_ml.ml_line_count; 4240 4241 if (curs > curbuf->b_ml.ml_line_count) 4242 curs = curbuf->b_ml.ml_line_count; 4243 4244 for (i = start; i <= end; i++) 4245 { 4246 if (minus && i == lnum) 4247 { 4248 msg_putchar('\n'); 4249 4250 for (j = 1; j < Columns; j++) 4251 msg_putchar('-'); 4252 } 4253 4254 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); 4255 4256 if (minus && i == lnum) 4257 { 4258 msg_putchar('\n'); 4259 4260 for (j = 1; j < Columns; j++) 4261 msg_putchar('-'); 4262 } 4263 } 4264 4265 curwin->w_cursor.lnum = curs; 4266 ex_no_reprint = TRUE; 4267 } 4268 4269 /* 4270 * Check if the restricted flag is set. 4271 * If so, give an error message and return TRUE. 4272 * Otherwise, return FALSE. 4273 */ 4274 int 4275 check_restricted(void) 4276 { 4277 if (restricted) 4278 { 4279 EMSG(_("E145: Shell commands not allowed in rvim")); 4280 return TRUE; 4281 } 4282 return FALSE; 4283 } 4284 4285 /* 4286 * Check if the secure flag is set (.exrc or .vimrc in current directory). 4287 * If so, give an error message and return TRUE. 4288 * Otherwise, return FALSE. 4289 */ 4290 int 4291 check_secure(void) 4292 { 4293 if (secure) 4294 { 4295 secure = 2; 4296 EMSG(_(e_curdir)); 4297 return TRUE; 4298 } 4299 #ifdef HAVE_SANDBOX 4300 /* 4301 * In the sandbox more things are not allowed, including the things 4302 * disallowed in secure mode. 4303 */ 4304 if (sandbox != 0) 4305 { 4306 EMSG(_(e_sandbox)); 4307 return TRUE; 4308 } 4309 #endif 4310 return FALSE; 4311 } 4312 4313 static char_u *old_sub = NULL; /* previous substitute pattern */ 4314 static int global_need_beginline; /* call beginline() after ":g" */ 4315 4316 /* do_sub() 4317 * 4318 * Perform a substitution from line eap->line1 to line eap->line2 using the 4319 * command pointed to by eap->arg which should be of the form: 4320 * 4321 * /pattern/substitution/{flags} 4322 * 4323 * The usual escapes are supported as described in the regexp docs. 4324 */ 4325 void 4326 do_sub(exarg_T *eap) 4327 { 4328 linenr_T lnum; 4329 long i = 0; 4330 regmmatch_T regmatch; 4331 static int do_all = FALSE; /* do multiple substitutions per line */ 4332 static int do_ask = FALSE; /* ask for confirmation */ 4333 static int do_count = FALSE; /* count only */ 4334 static int do_error = TRUE; /* if false, ignore errors */ 4335 static int do_print = FALSE; /* print last line with subs. */ 4336 static int do_list = FALSE; /* list last line with subs. */ 4337 static int do_number = FALSE; /* list last line with line nr*/ 4338 static int do_ic = 0; /* ignore case flag */ 4339 int save_do_all; /* remember user specified 'g' flag */ 4340 int save_do_ask; /* remember user specified 'c' flag */ 4341 char_u *pat = NULL, *sub = NULL; /* init for GCC */ 4342 int delimiter; 4343 int sublen; 4344 int got_quit = FALSE; 4345 int got_match = FALSE; 4346 int temp; 4347 int which_pat; 4348 char_u *cmd; 4349 int save_State; 4350 linenr_T first_line = 0; /* first changed line */ 4351 linenr_T last_line= 0; /* below last changed line AFTER the 4352 * change */ 4353 linenr_T old_line_count = curbuf->b_ml.ml_line_count; 4354 linenr_T line2; 4355 long nmatch; /* number of lines in match */ 4356 char_u *sub_firstline; /* allocated copy of first sub line */ 4357 int endcolumn = FALSE; /* cursor in last column when done */ 4358 pos_T old_cursor = curwin->w_cursor; 4359 int start_nsubs; 4360 #ifdef FEAT_EVAL 4361 int save_ma = 0; 4362 #endif 4363 4364 cmd = eap->arg; 4365 if (!global_busy) 4366 { 4367 sub_nsubs = 0; 4368 sub_nlines = 0; 4369 } 4370 start_nsubs = sub_nsubs; 4371 4372 if (eap->cmdidx == CMD_tilde) 4373 which_pat = RE_LAST; /* use last used regexp */ 4374 else 4375 which_pat = RE_SUBST; /* use last substitute regexp */ 4376 4377 /* new pattern and substitution */ 4378 if (eap->cmd[0] == 's' && *cmd != NUL && !vim_iswhite(*cmd) 4379 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) 4380 { 4381 /* don't accept alphanumeric for separator */ 4382 if (isalpha(*cmd)) 4383 { 4384 EMSG(_("E146: Regular expressions can't be delimited by letters")); 4385 return; 4386 } 4387 /* 4388 * undocumented vi feature: 4389 * "\/sub/" and "\?sub?" use last used search pattern (almost like 4390 * //sub/r). "\&sub&" use last substitute pattern (like //sub/). 4391 */ 4392 if (*cmd == '\\') 4393 { 4394 ++cmd; 4395 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) 4396 { 4397 EMSG(_(e_backslash)); 4398 return; 4399 } 4400 if (*cmd != '&') 4401 which_pat = RE_SEARCH; /* use last '/' pattern */ 4402 pat = (char_u *)""; /* empty search pattern */ 4403 delimiter = *cmd++; /* remember delimiter character */ 4404 } 4405 else /* find the end of the regexp */ 4406 { 4407 #ifdef FEAT_FKMAP /* reverse the flow of the Farsi characters */ 4408 if (p_altkeymap && curwin->w_p_rl) 4409 lrF_sub(cmd); 4410 #endif 4411 which_pat = RE_LAST; /* use last used regexp */ 4412 delimiter = *cmd++; /* remember delimiter character */ 4413 pat = cmd; /* remember start of search pat */ 4414 cmd = skip_regexp(cmd, delimiter, p_magic, &eap->arg); 4415 if (cmd[0] == delimiter) /* end delimiter found */ 4416 *cmd++ = NUL; /* replace it with a NUL */ 4417 } 4418 4419 /* 4420 * Small incompatibility: vi sees '\n' as end of the command, but in 4421 * Vim we want to use '\n' to find/substitute a NUL. 4422 */ 4423 sub = cmd; /* remember the start of the substitution */ 4424 4425 while (cmd[0]) 4426 { 4427 if (cmd[0] == delimiter) /* end delimiter found */ 4428 { 4429 *cmd++ = NUL; /* replace it with a NUL */ 4430 break; 4431 } 4432 if (cmd[0] == '\\' && cmd[1] != 0) /* skip escaped characters */ 4433 ++cmd; 4434 mb_ptr_adv(cmd); 4435 } 4436 4437 if (!eap->skip) 4438 { 4439 /* In POSIX vi ":s/pat/%/" uses the previous subst. string. */ 4440 if (STRCMP(sub, "%") == 0 4441 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) 4442 { 4443 if (old_sub == NULL) /* there is no previous command */ 4444 { 4445 EMSG(_(e_nopresub)); 4446 return; 4447 } 4448 sub = old_sub; 4449 } 4450 else 4451 { 4452 vim_free(old_sub); 4453 old_sub = vim_strsave(sub); 4454 } 4455 } 4456 } 4457 else if (!eap->skip) /* use previous pattern and substitution */ 4458 { 4459 if (old_sub == NULL) /* there is no previous command */ 4460 { 4461 EMSG(_(e_nopresub)); 4462 return; 4463 } 4464 pat = NULL; /* search_regcomp() will use previous pattern */ 4465 sub = old_sub; 4466 4467 /* Vi compatibility quirk: repeating with ":s" keeps the cursor in the 4468 * last column after using "$". */ 4469 endcolumn = (curwin->w_curswant == MAXCOL); 4470 } 4471 4472 /* Recognize ":%s/\n//" and turn it into a join command, which is much 4473 * more efficient. 4474 * TODO: find a generic solution to make line-joining operations more 4475 * efficient, avoid allocating a string that grows in size. 4476 */ 4477 if (pat != NULL && STRCMP(pat, "\\n") == 0 4478 && *sub == NUL 4479 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l' 4480 || *cmd == 'p' || *cmd == '#')))) 4481 { 4482 linenr_T joined_lines_count; 4483 4484 curwin->w_cursor.lnum = eap->line1; 4485 if (*cmd == 'l') 4486 eap->flags = EXFLAG_LIST; 4487 else if (*cmd == '#') 4488 eap->flags = EXFLAG_NR; 4489 else if (*cmd == 'p') 4490 eap->flags = EXFLAG_PRINT; 4491 4492 /* The number of lines joined is the number of lines in the range plus 4493 * one. One less when the last line is included. */ 4494 joined_lines_count = eap->line2 - eap->line1 + 1; 4495 if (eap->line2 < curbuf->b_ml.ml_line_count) 4496 ++joined_lines_count; 4497 if (joined_lines_count > 1) 4498 { 4499 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE); 4500 sub_nsubs = joined_lines_count - 1; 4501 sub_nlines = 1; 4502 (void)do_sub_msg(FALSE); 4503 ex_may_print(eap); 4504 } 4505 4506 if (!cmdmod.keeppatterns) 4507 save_re_pat(RE_SUBST, pat, p_magic); 4508 #ifdef FEAT_CMDHIST 4509 /* put pattern in history */ 4510 add_to_history(HIST_SEARCH, pat, TRUE, NUL); 4511 #endif 4512 4513 return; 4514 } 4515 4516 /* 4517 * Find trailing options. When '&' is used, keep old options. 4518 */ 4519 if (*cmd == '&') 4520 ++cmd; 4521 else 4522 { 4523 if (!p_ed) 4524 { 4525 if (p_gd) /* default is global on */ 4526 do_all = TRUE; 4527 else 4528 do_all = FALSE; 4529 do_ask = FALSE; 4530 } 4531 do_error = TRUE; 4532 do_print = FALSE; 4533 do_count = FALSE; 4534 do_number = FALSE; 4535 do_ic = 0; 4536 } 4537 while (*cmd) 4538 { 4539 /* 4540 * Note that 'g' and 'c' are always inverted, also when p_ed is off. 4541 * 'r' is never inverted. 4542 */ 4543 if (*cmd == 'g') 4544 do_all = !do_all; 4545 else if (*cmd == 'c') 4546 do_ask = !do_ask; 4547 else if (*cmd == 'n') 4548 do_count = TRUE; 4549 else if (*cmd == 'e') 4550 do_error = !do_error; 4551 else if (*cmd == 'r') /* use last used regexp */ 4552 which_pat = RE_LAST; 4553 else if (*cmd == 'p') 4554 do_print = TRUE; 4555 else if (*cmd == '#') 4556 { 4557 do_print = TRUE; 4558 do_number = TRUE; 4559 } 4560 else if (*cmd == 'l') 4561 { 4562 do_print = TRUE; 4563 do_list = TRUE; 4564 } 4565 else if (*cmd == 'i') /* ignore case */ 4566 do_ic = 'i'; 4567 else if (*cmd == 'I') /* don't ignore case */ 4568 do_ic = 'I'; 4569 else 4570 break; 4571 ++cmd; 4572 } 4573 if (do_count) 4574 do_ask = FALSE; 4575 4576 save_do_all = do_all; 4577 save_do_ask = do_ask; 4578 4579 /* 4580 * check for a trailing count 4581 */ 4582 cmd = skipwhite(cmd); 4583 if (VIM_ISDIGIT(*cmd)) 4584 { 4585 i = getdigits(&cmd); 4586 if (i <= 0 && !eap->skip && do_error) 4587 { 4588 EMSG(_(e_zerocount)); 4589 return; 4590 } 4591 eap->line1 = eap->line2; 4592 eap->line2 += i - 1; 4593 if (eap->line2 > curbuf->b_ml.ml_line_count) 4594 eap->line2 = curbuf->b_ml.ml_line_count; 4595 } 4596 4597 /* 4598 * check for trailing command or garbage 4599 */ 4600 cmd = skipwhite(cmd); 4601 if (*cmd && *cmd != '"') /* if not end-of-line or comment */ 4602 { 4603 eap->nextcmd = check_nextcmd(cmd); 4604 if (eap->nextcmd == NULL) 4605 { 4606 EMSG(_(e_trailing)); 4607 return; 4608 } 4609 } 4610 4611 if (eap->skip) /* not executing commands, only parsing */ 4612 return; 4613 4614 if (!do_count && !curbuf->b_p_ma) 4615 { 4616 /* Substitution is not allowed in non-'modifiable' buffer */ 4617 EMSG(_(e_modifiable)); 4618 return; 4619 } 4620 4621 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) 4622 { 4623 if (do_error) 4624 EMSG(_(e_invcmd)); 4625 return; 4626 } 4627 4628 /* the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' */ 4629 if (do_ic == 'i') 4630 regmatch.rmm_ic = TRUE; 4631 else if (do_ic == 'I') 4632 regmatch.rmm_ic = FALSE; 4633 4634 sub_firstline = NULL; 4635 4636 /* 4637 * ~ in the substitute pattern is replaced with the old pattern. 4638 * We do it here once to avoid it to be replaced over and over again. 4639 * But don't do it when it starts with "\=", then it's an expression. 4640 */ 4641 if (!(sub[0] == '\\' && sub[1] == '=')) 4642 sub = regtilde(sub, p_magic); 4643 4644 /* 4645 * Check for a match on each line. 4646 */ 4647 line2 = eap->line2; 4648 for (lnum = eap->line1; lnum <= line2 && !(got_quit 4649 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) 4650 || aborting() 4651 #endif 4652 ); ++lnum) 4653 { 4654 nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, 4655 (colnr_T)0, NULL); 4656 if (nmatch) 4657 { 4658 colnr_T copycol; 4659 colnr_T matchcol; 4660 colnr_T prev_matchcol = MAXCOL; 4661 char_u *new_end, *new_start = NULL; 4662 unsigned new_start_len = 0; 4663 char_u *p1; 4664 int did_sub = FALSE; 4665 int lastone; 4666 int len, copy_len, needed_len; 4667 long nmatch_tl = 0; /* nr of lines matched below lnum */ 4668 int do_again; /* do it again after joining lines */ 4669 int skip_match = FALSE; 4670 linenr_T sub_firstlnum; /* nr of first sub line */ 4671 4672 /* 4673 * The new text is build up step by step, to avoid too much 4674 * copying. There are these pieces: 4675 * sub_firstline The old text, unmodified. 4676 * copycol Column in the old text where we started 4677 * looking for a match; from here old text still 4678 * needs to be copied to the new text. 4679 * matchcol Column number of the old text where to look 4680 * for the next match. It's just after the 4681 * previous match or one further. 4682 * prev_matchcol Column just after the previous match (if any). 4683 * Mostly equal to matchcol, except for the first 4684 * match and after skipping an empty match. 4685 * regmatch.*pos Where the pattern matched in the old text. 4686 * new_start The new text, all that has been produced so 4687 * far. 4688 * new_end The new text, where to append new text. 4689 * 4690 * lnum The line number where we found the start of 4691 * the match. Can be below the line we searched 4692 * when there is a \n before a \zs in the 4693 * pattern. 4694 * sub_firstlnum The line number in the buffer where to look 4695 * for a match. Can be different from "lnum" 4696 * when the pattern or substitute string contains 4697 * line breaks. 4698 * 4699 * Special situations: 4700 * - When the substitute string contains a line break, the part up 4701 * to the line break is inserted in the text, but the copy of 4702 * the original line is kept. "sub_firstlnum" is adjusted for 4703 * the inserted lines. 4704 * - When the matched pattern contains a line break, the old line 4705 * is taken from the line at the end of the pattern. The lines 4706 * in the match are deleted later, "sub_firstlnum" is adjusted 4707 * accordingly. 4708 * 4709 * The new text is built up in new_start[]. It has some extra 4710 * room to avoid using alloc()/free() too often. new_start_len is 4711 * the length of the allocated memory at new_start. 4712 * 4713 * Make a copy of the old line, so it won't be taken away when 4714 * updating the screen or handling a multi-line match. The "old_" 4715 * pointers point into this copy. 4716 */ 4717 sub_firstlnum = lnum; 4718 copycol = 0; 4719 matchcol = 0; 4720 4721 /* At first match, remember current cursor position. */ 4722 if (!got_match) 4723 { 4724 setpcmark(); 4725 got_match = TRUE; 4726 } 4727 4728 /* 4729 * Loop until nothing more to replace in this line. 4730 * 1. Handle match with empty string. 4731 * 2. If do_ask is set, ask for confirmation. 4732 * 3. substitute the string. 4733 * 4. if do_all is set, find next match 4734 * 5. break if there isn't another match in this line 4735 */ 4736 for (;;) 4737 { 4738 /* Advance "lnum" to the line where the match starts. The 4739 * match does not start in the first line when there is a line 4740 * break before \zs. */ 4741 if (regmatch.startpos[0].lnum > 0) 4742 { 4743 lnum += regmatch.startpos[0].lnum; 4744 sub_firstlnum += regmatch.startpos[0].lnum; 4745 nmatch -= regmatch.startpos[0].lnum; 4746 vim_free(sub_firstline); 4747 sub_firstline = NULL; 4748 } 4749 4750 if (sub_firstline == NULL) 4751 { 4752 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); 4753 if (sub_firstline == NULL) 4754 { 4755 vim_free(new_start); 4756 goto outofmem; 4757 } 4758 } 4759 4760 /* Save the line number of the last change for the final 4761 * cursor position (just like Vi). */ 4762 curwin->w_cursor.lnum = lnum; 4763 do_again = FALSE; 4764 4765 /* 4766 * 1. Match empty string does not count, except for first 4767 * match. This reproduces the strange vi behaviour. 4768 * This also catches endless loops. 4769 */ 4770 if (matchcol == prev_matchcol 4771 && regmatch.endpos[0].lnum == 0 4772 && matchcol == regmatch.endpos[0].col) 4773 { 4774 if (sub_firstline[matchcol] == NUL) 4775 /* We already were at the end of the line. Don't look 4776 * for a match in this line again. */ 4777 skip_match = TRUE; 4778 else 4779 { 4780 /* search for a match at next column */ 4781 #ifdef FEAT_MBYTE 4782 if (has_mbyte) 4783 matchcol += mb_ptr2len(sub_firstline + matchcol); 4784 else 4785 #endif 4786 ++matchcol; 4787 } 4788 goto skip; 4789 } 4790 4791 /* Normally we continue searching for a match just after the 4792 * previous match. */ 4793 matchcol = regmatch.endpos[0].col; 4794 prev_matchcol = matchcol; 4795 4796 /* 4797 * 2. If do_count is set only increase the counter. 4798 * If do_ask is set, ask for confirmation. 4799 */ 4800 if (do_count) 4801 { 4802 /* For a multi-line match, put matchcol at the NUL at 4803 * the end of the line and set nmatch to one, so that 4804 * we continue looking for a match on the next line. 4805 * Avoids that ":s/\nB\@=//gc" get stuck. */ 4806 if (nmatch > 1) 4807 { 4808 matchcol = (colnr_T)STRLEN(sub_firstline); 4809 nmatch = 1; 4810 skip_match = TRUE; 4811 } 4812 sub_nsubs++; 4813 did_sub = TRUE; 4814 #ifdef FEAT_EVAL 4815 /* Skip the substitution, unless an expression is used, 4816 * then it is evaluated in the sandbox. */ 4817 if (!(sub[0] == '\\' && sub[1] == '=')) 4818 #endif 4819 goto skip; 4820 } 4821 4822 if (do_ask) 4823 { 4824 int typed = 0; 4825 4826 /* change State to CONFIRM, so that the mouse works 4827 * properly */ 4828 save_State = State; 4829 State = CONFIRM; 4830 #ifdef FEAT_MOUSE 4831 setmouse(); /* disable mouse in xterm */ 4832 #endif 4833 curwin->w_cursor.col = regmatch.startpos[0].col; 4834 4835 /* When 'cpoptions' contains "u" don't sync undo when 4836 * asking for confirmation. */ 4837 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) 4838 ++no_u_sync; 4839 4840 /* 4841 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. 4842 */ 4843 while (do_ask) 4844 { 4845 if (exmode_active) 4846 { 4847 char_u *resp; 4848 colnr_T sc, ec; 4849 4850 print_line_no_prefix(lnum, do_number, do_list); 4851 4852 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); 4853 curwin->w_cursor.col = regmatch.endpos[0].col - 1; 4854 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); 4855 if (do_number || curwin->w_p_nu) 4856 { 4857 int numw = number_width(curwin) + 1; 4858 sc += numw; 4859 ec += numw; 4860 } 4861 msg_start(); 4862 for (i = 0; i < (long)sc; ++i) 4863 msg_putchar(' '); 4864 for ( ; i <= (long)ec; ++i) 4865 msg_putchar('^'); 4866 4867 resp = getexmodeline('?', NULL, 0); 4868 if (resp != NULL) 4869 { 4870 typed = *resp; 4871 vim_free(resp); 4872 } 4873 } 4874 else 4875 { 4876 char_u *orig_line = NULL; 4877 int len_change = 0; 4878 #ifdef FEAT_FOLDING 4879 int save_p_fen = curwin->w_p_fen; 4880 4881 curwin->w_p_fen = FALSE; 4882 #endif 4883 /* Invert the matched string. 4884 * Remove the inversion afterwards. */ 4885 temp = RedrawingDisabled; 4886 RedrawingDisabled = 0; 4887 4888 if (new_start != NULL) 4889 { 4890 /* There already was a substitution, we would 4891 * like to show this to the user. We cannot 4892 * really update the line, it would change 4893 * what matches. Temporarily replace the line 4894 * and change it back afterwards. */ 4895 orig_line = vim_strsave(ml_get(lnum)); 4896 if (orig_line != NULL) 4897 { 4898 char_u *new_line = concat_str(new_start, 4899 sub_firstline + copycol); 4900 4901 if (new_line == NULL) 4902 { 4903 vim_free(orig_line); 4904 orig_line = NULL; 4905 } 4906 else 4907 { 4908 /* Position the cursor relative to the 4909 * end of the line, the previous 4910 * substitute may have inserted or 4911 * deleted characters before the 4912 * cursor. */ 4913 len_change = (int)STRLEN(new_line) 4914 - (int)STRLEN(orig_line); 4915 curwin->w_cursor.col += len_change; 4916 ml_replace(lnum, new_line, FALSE); 4917 } 4918 } 4919 } 4920 4921 search_match_lines = regmatch.endpos[0].lnum 4922 - regmatch.startpos[0].lnum; 4923 search_match_endcol = regmatch.endpos[0].col 4924 + len_change; 4925 highlight_match = TRUE; 4926 4927 update_topline(); 4928 validate_cursor(); 4929 update_screen(SOME_VALID); 4930 highlight_match = FALSE; 4931 redraw_later(SOME_VALID); 4932 4933 #ifdef FEAT_FOLDING 4934 curwin->w_p_fen = save_p_fen; 4935 #endif 4936 if (msg_row == Rows - 1) 4937 msg_didout = FALSE; /* avoid a scroll-up */ 4938 msg_starthere(); 4939 i = msg_scroll; 4940 msg_scroll = 0; /* truncate msg when 4941 needed */ 4942 msg_no_more = TRUE; 4943 /* write message same highlighting as for 4944 * wait_return */ 4945 smsg_attr(hl_attr(HLF_R), 4946 (char_u *)_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); 4947 msg_no_more = FALSE; 4948 msg_scroll = i; 4949 showruler(TRUE); 4950 windgoto(msg_row, msg_col); 4951 RedrawingDisabled = temp; 4952 4953 #ifdef USE_ON_FLY_SCROLL 4954 dont_scroll = FALSE; /* allow scrolling here */ 4955 #endif 4956 ++no_mapping; /* don't map this key */ 4957 ++allow_keys; /* allow special keys */ 4958 typed = plain_vgetc(); 4959 --allow_keys; 4960 --no_mapping; 4961 4962 /* clear the question */ 4963 msg_didout = FALSE; /* don't scroll up */ 4964 msg_col = 0; 4965 gotocmdline(TRUE); 4966 4967 /* restore the line */ 4968 if (orig_line != NULL) 4969 ml_replace(lnum, orig_line, FALSE); 4970 } 4971 4972 need_wait_return = FALSE; /* no hit-return prompt */ 4973 if (typed == 'q' || typed == ESC || typed == Ctrl_C 4974 #ifdef UNIX 4975 || typed == intr_char 4976 #endif 4977 ) 4978 { 4979 got_quit = TRUE; 4980 break; 4981 } 4982 if (typed == 'n') 4983 break; 4984 if (typed == 'y') 4985 break; 4986 if (typed == 'l') 4987 { 4988 /* last: replace and then stop */ 4989 do_all = FALSE; 4990 line2 = lnum; 4991 break; 4992 } 4993 if (typed == 'a') 4994 { 4995 do_ask = FALSE; 4996 break; 4997 } 4998 #ifdef FEAT_INS_EXPAND 4999 if (typed == Ctrl_E) 5000 scrollup_clamp(); 5001 else if (typed == Ctrl_Y) 5002 scrolldown_clamp(); 5003 #endif 5004 } 5005 State = save_State; 5006 #ifdef FEAT_MOUSE 5007 setmouse(); 5008 #endif 5009 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) 5010 --no_u_sync; 5011 5012 if (typed == 'n') 5013 { 5014 /* For a multi-line match, put matchcol at the NUL at 5015 * the end of the line and set nmatch to one, so that 5016 * we continue looking for a match on the next line. 5017 * Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc" 5018 * get stuck when pressing 'n'. */ 5019 if (nmatch > 1) 5020 { 5021 matchcol = (colnr_T)STRLEN(sub_firstline); 5022 skip_match = TRUE; 5023 } 5024 goto skip; 5025 } 5026 if (got_quit) 5027 goto skip; 5028 } 5029 5030 /* Move the cursor to the start of the match, so that we can 5031 * use "\=col("."). */ 5032 curwin->w_cursor.col = regmatch.startpos[0].col; 5033 5034 /* 5035 * 3. substitute the string. 5036 */ 5037 #ifdef FEAT_EVAL 5038 if (do_count) 5039 { 5040 /* prevent accidentally changing the buffer by a function */ 5041 save_ma = curbuf->b_p_ma; 5042 curbuf->b_p_ma = FALSE; 5043 sandbox++; 5044 } 5045 #endif 5046 /* get length of substitution part */ 5047 sublen = vim_regsub_multi(®match, 5048 sub_firstlnum - regmatch.startpos[0].lnum, 5049 sub, sub_firstline, FALSE, p_magic, TRUE); 5050 #ifdef FEAT_EVAL 5051 if (do_count) 5052 { 5053 curbuf->b_p_ma = save_ma; 5054 sandbox--; 5055 goto skip; 5056 } 5057 #endif 5058 5059 /* When the match included the "$" of the last line it may 5060 * go beyond the last line of the buffer. */ 5061 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) 5062 { 5063 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; 5064 skip_match = TRUE; 5065 } 5066 5067 /* Need room for: 5068 * - result so far in new_start (not for first sub in line) 5069 * - original text up to match 5070 * - length of substituted part 5071 * - original text after match 5072 */ 5073 if (nmatch == 1) 5074 p1 = sub_firstline; 5075 else 5076 { 5077 p1 = ml_get(sub_firstlnum + nmatch - 1); 5078 nmatch_tl += nmatch - 1; 5079 } 5080 copy_len = regmatch.startpos[0].col - copycol; 5081 needed_len = copy_len + ((unsigned)STRLEN(p1) 5082 - regmatch.endpos[0].col) + sublen + 1; 5083 if (new_start == NULL) 5084 { 5085 /* 5086 * Get some space for a temporary buffer to do the 5087 * substitution into (and some extra space to avoid 5088 * too many calls to alloc()/free()). 5089 */ 5090 new_start_len = needed_len + 50; 5091 if ((new_start = alloc_check(new_start_len)) == NULL) 5092 goto outofmem; 5093 *new_start = NUL; 5094 new_end = new_start; 5095 } 5096 else 5097 { 5098 /* 5099 * Check if the temporary buffer is long enough to do the 5100 * substitution into. If not, make it larger (with a bit 5101 * extra to avoid too many calls to alloc()/free()). 5102 */ 5103 len = (unsigned)STRLEN(new_start); 5104 needed_len += len; 5105 if (needed_len > (int)new_start_len) 5106 { 5107 new_start_len = needed_len + 50; 5108 if ((p1 = alloc_check(new_start_len)) == NULL) 5109 { 5110 vim_free(new_start); 5111 goto outofmem; 5112 } 5113 mch_memmove(p1, new_start, (size_t)(len + 1)); 5114 vim_free(new_start); 5115 new_start = p1; 5116 } 5117 new_end = new_start + len; 5118 } 5119 5120 /* 5121 * copy the text up to the part that matched 5122 */ 5123 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len); 5124 new_end += copy_len; 5125 5126 (void)vim_regsub_multi(®match, 5127 sub_firstlnum - regmatch.startpos[0].lnum, 5128 sub, new_end, TRUE, p_magic, TRUE); 5129 sub_nsubs++; 5130 did_sub = TRUE; 5131 5132 /* Move the cursor to the start of the line, to avoid that it 5133 * is beyond the end of the line after the substitution. */ 5134 curwin->w_cursor.col = 0; 5135 5136 /* For a multi-line match, make a copy of the last matched 5137 * line and continue in that one. */ 5138 if (nmatch > 1) 5139 { 5140 sub_firstlnum += nmatch - 1; 5141 vim_free(sub_firstline); 5142 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); 5143 /* When going beyond the last line, stop substituting. */ 5144 if (sub_firstlnum <= line2) 5145 do_again = TRUE; 5146 else 5147 do_all = FALSE; 5148 } 5149 5150 /* Remember next character to be copied. */ 5151 copycol = regmatch.endpos[0].col; 5152 5153 if (skip_match) 5154 { 5155 /* Already hit end of the buffer, sub_firstlnum is one 5156 * less than what it ought to be. */ 5157 vim_free(sub_firstline); 5158 sub_firstline = vim_strsave((char_u *)""); 5159 copycol = 0; 5160 } 5161 5162 /* 5163 * Now the trick is to replace CTRL-M chars with a real line 5164 * break. This would make it impossible to insert a CTRL-M in 5165 * the text. The line break can be avoided by preceding the 5166 * CTRL-M with a backslash. To be able to insert a backslash, 5167 * they must be doubled in the string and are halved here. 5168 * That is Vi compatible. 5169 */ 5170 for (p1 = new_end; *p1; ++p1) 5171 { 5172 if (p1[0] == '\\' && p1[1] != NUL) /* remove backslash */ 5173 STRMOVE(p1, p1 + 1); 5174 else if (*p1 == CAR) 5175 { 5176 if (u_inssub(lnum) == OK) /* prepare for undo */ 5177 { 5178 *p1 = NUL; /* truncate up to the CR */ 5179 ml_append(lnum - 1, new_start, 5180 (colnr_T)(p1 - new_start + 1), FALSE); 5181 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); 5182 if (do_ask) 5183 appended_lines(lnum - 1, 1L); 5184 else 5185 { 5186 if (first_line == 0) 5187 first_line = lnum; 5188 last_line = lnum + 1; 5189 } 5190 /* All line numbers increase. */ 5191 ++sub_firstlnum; 5192 ++lnum; 5193 ++line2; 5194 /* move the cursor to the new line, like Vi */ 5195 ++curwin->w_cursor.lnum; 5196 /* copy the rest */ 5197 STRMOVE(new_start, p1 + 1); 5198 p1 = new_start - 1; 5199 } 5200 } 5201 #ifdef FEAT_MBYTE 5202 else if (has_mbyte) 5203 p1 += (*mb_ptr2len)(p1) - 1; 5204 #endif 5205 } 5206 5207 /* 5208 * 4. If do_all is set, find next match. 5209 * Prevent endless loop with patterns that match empty 5210 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. 5211 * But ":s/\n/#/" is OK. 5212 */ 5213 skip: 5214 /* We already know that we did the last subst when we are at 5215 * the end of the line, except that a pattern like 5216 * "bar\|\nfoo" may match at the NUL. "lnum" can be below 5217 * "line2" when there is a \zs in the pattern after a line 5218 * break. */ 5219 lastone = (skip_match 5220 || got_int 5221 || got_quit 5222 || lnum > line2 5223 || !(do_all || do_again) 5224 || (sub_firstline[matchcol] == NUL && nmatch <= 1 5225 && !re_multiline(regmatch.regprog))); 5226 nmatch = -1; 5227 5228 /* 5229 * Replace the line in the buffer when needed. This is 5230 * skipped when there are more matches. 5231 * The check for nmatch_tl is needed for when multi-line 5232 * matching must replace the lines before trying to do another 5233 * match, otherwise "\@<=" won't work. 5234 * When the match starts below where we start searching also 5235 * need to replace the line first (using \zs after \n). 5236 */ 5237 if (lastone 5238 || nmatch_tl > 0 5239 || (nmatch = vim_regexec_multi(®match, curwin, 5240 curbuf, sub_firstlnum, 5241 matchcol, NULL)) == 0 5242 || regmatch.startpos[0].lnum > 0) 5243 { 5244 if (new_start != NULL) 5245 { 5246 /* 5247 * Copy the rest of the line, that didn't match. 5248 * "matchcol" has to be adjusted, we use the end of 5249 * the line as reference, because the substitute may 5250 * have changed the number of characters. Same for 5251 * "prev_matchcol". 5252 */ 5253 STRCAT(new_start, sub_firstline + copycol); 5254 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; 5255 prev_matchcol = (colnr_T)STRLEN(sub_firstline) 5256 - prev_matchcol; 5257 5258 if (u_savesub(lnum) != OK) 5259 break; 5260 ml_replace(lnum, new_start, TRUE); 5261 5262 if (nmatch_tl > 0) 5263 { 5264 /* 5265 * Matched lines have now been substituted and are 5266 * useless, delete them. The part after the match 5267 * has been appended to new_start, we don't need 5268 * it in the buffer. 5269 */ 5270 ++lnum; 5271 if (u_savedel(lnum, nmatch_tl) != OK) 5272 break; 5273 for (i = 0; i < nmatch_tl; ++i) 5274 ml_delete(lnum, (int)FALSE); 5275 mark_adjust(lnum, lnum + nmatch_tl - 1, 5276 (long)MAXLNUM, -nmatch_tl); 5277 if (do_ask) 5278 deleted_lines(lnum, nmatch_tl); 5279 --lnum; 5280 line2 -= nmatch_tl; /* nr of lines decreases */ 5281 nmatch_tl = 0; 5282 } 5283 5284 /* When asking, undo is saved each time, must also set 5285 * changed flag each time. */ 5286 if (do_ask) 5287 changed_bytes(lnum, 0); 5288 else 5289 { 5290 if (first_line == 0) 5291 first_line = lnum; 5292 last_line = lnum + 1; 5293 } 5294 5295 sub_firstlnum = lnum; 5296 vim_free(sub_firstline); /* free the temp buffer */ 5297 sub_firstline = new_start; 5298 new_start = NULL; 5299 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; 5300 prev_matchcol = (colnr_T)STRLEN(sub_firstline) 5301 - prev_matchcol; 5302 copycol = 0; 5303 } 5304 if (nmatch == -1 && !lastone) 5305 nmatch = vim_regexec_multi(®match, curwin, curbuf, 5306 sub_firstlnum, matchcol, NULL); 5307 5308 /* 5309 * 5. break if there isn't another match in this line 5310 */ 5311 if (nmatch <= 0) 5312 { 5313 /* If the match found didn't start where we were 5314 * searching, do the next search in the line where we 5315 * found the match. */ 5316 if (nmatch == -1) 5317 lnum -= regmatch.startpos[0].lnum; 5318 break; 5319 } 5320 } 5321 5322 line_breakcheck(); 5323 } 5324 5325 if (did_sub) 5326 ++sub_nlines; 5327 vim_free(new_start); /* for when substitute was cancelled */ 5328 vim_free(sub_firstline); /* free the copy of the original line */ 5329 sub_firstline = NULL; 5330 } 5331 5332 line_breakcheck(); 5333 } 5334 5335 if (first_line != 0) 5336 { 5337 /* Need to subtract the number of added lines from "last_line" to get 5338 * the line number before the change (same as adding the number of 5339 * deleted lines). */ 5340 i = curbuf->b_ml.ml_line_count - old_line_count; 5341 changed_lines(first_line, 0, last_line - i, i); 5342 } 5343 5344 outofmem: 5345 vim_free(sub_firstline); /* may have to free allocated copy of the line */ 5346 5347 /* ":s/pat//n" doesn't move the cursor */ 5348 if (do_count) 5349 curwin->w_cursor = old_cursor; 5350 5351 if (sub_nsubs > start_nsubs) 5352 { 5353 /* Set the '[ and '] marks. */ 5354 curbuf->b_op_start.lnum = eap->line1; 5355 curbuf->b_op_end.lnum = line2; 5356 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 5357 5358 if (!global_busy) 5359 { 5360 if (!do_ask) /* when interactive leave cursor on the match */ 5361 { 5362 if (endcolumn) 5363 coladvance((colnr_T)MAXCOL); 5364 else 5365 beginline(BL_WHITE | BL_FIX); 5366 } 5367 if (!do_sub_msg(do_count) && do_ask) 5368 MSG(""); 5369 } 5370 else 5371 global_need_beginline = TRUE; 5372 if (do_print) 5373 print_line(curwin->w_cursor.lnum, do_number, do_list); 5374 } 5375 else if (!global_busy) 5376 { 5377 if (got_int) /* interrupted */ 5378 EMSG(_(e_interr)); 5379 else if (got_match) /* did find something but nothing substituted */ 5380 MSG(""); 5381 else if (do_error) /* nothing found */ 5382 EMSG2(_(e_patnotf2), get_search_pat()); 5383 } 5384 5385 #ifdef FEAT_FOLDING 5386 if (do_ask && hasAnyFolding(curwin)) 5387 /* Cursor position may require updating */ 5388 changed_window_setting(); 5389 #endif 5390 5391 vim_regfree(regmatch.regprog); 5392 5393 /* Restore the flag values, they can be used for ":&&". */ 5394 do_all = save_do_all; 5395 do_ask = save_do_ask; 5396 } 5397 5398 /* 5399 * Give message for number of substitutions. 5400 * Can also be used after a ":global" command. 5401 * Return TRUE if a message was given. 5402 */ 5403 int 5404 do_sub_msg( 5405 int count_only) /* used 'n' flag for ":s" */ 5406 { 5407 /* 5408 * Only report substitutions when: 5409 * - more than 'report' substitutions 5410 * - command was typed by user, or number of changed lines > 'report' 5411 * - giving messages is not disabled by 'lazyredraw' 5412 */ 5413 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) 5414 || count_only) 5415 && messaging()) 5416 { 5417 if (got_int) 5418 STRCPY(msg_buf, _("(Interrupted) ")); 5419 else 5420 *msg_buf = NUL; 5421 if (sub_nsubs == 1) 5422 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), 5423 "%s", count_only ? _("1 match") : _("1 substitution")); 5424 else 5425 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), 5426 count_only ? _("%ld matches") : _("%ld substitutions"), 5427 sub_nsubs); 5428 if (sub_nlines == 1) 5429 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), 5430 "%s", _(" on 1 line")); 5431 else 5432 vim_snprintf_add((char *)msg_buf, sizeof(msg_buf), 5433 _(" on %ld lines"), (long)sub_nlines); 5434 if (msg(msg_buf)) 5435 /* save message to display it after redraw */ 5436 set_keep_msg(msg_buf, 0); 5437 return TRUE; 5438 } 5439 if (got_int) 5440 { 5441 EMSG(_(e_interr)); 5442 return TRUE; 5443 } 5444 return FALSE; 5445 } 5446 5447 /* 5448 * Execute a global command of the form: 5449 * 5450 * g/pattern/X : execute X on all lines where pattern matches 5451 * v/pattern/X : execute X on all lines where pattern does not match 5452 * 5453 * where 'X' is an EX command 5454 * 5455 * The command character (as well as the trailing slash) is optional, and 5456 * is assumed to be 'p' if missing. 5457 * 5458 * This is implemented in two passes: first we scan the file for the pattern and 5459 * set a mark for each line that (not) matches. Secondly we execute the command 5460 * for each line that has a mark. This is required because after deleting 5461 * lines we do not know where to search for the next match. 5462 */ 5463 void 5464 ex_global(exarg_T *eap) 5465 { 5466 linenr_T lnum; /* line number according to old situation */ 5467 int ndone = 0; 5468 int type; /* first char of cmd: 'v' or 'g' */ 5469 char_u *cmd; /* command argument */ 5470 5471 char_u delim; /* delimiter, normally '/' */ 5472 char_u *pat; 5473 regmmatch_T regmatch; 5474 int match; 5475 int which_pat; 5476 5477 if (global_busy) 5478 { 5479 EMSG(_("E147: Cannot do :global recursive")); /* will increment global_busy */ 5480 return; 5481 } 5482 5483 if (eap->forceit) /* ":global!" is like ":vglobal" */ 5484 type = 'v'; 5485 else 5486 type = *eap->cmd; 5487 cmd = eap->arg; 5488 which_pat = RE_LAST; /* default: use last used regexp */ 5489 5490 /* 5491 * undocumented vi feature: 5492 * "\/" and "\?": use previous search pattern. 5493 * "\&": use previous substitute pattern. 5494 */ 5495 if (*cmd == '\\') 5496 { 5497 ++cmd; 5498 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) 5499 { 5500 EMSG(_(e_backslash)); 5501 return; 5502 } 5503 if (*cmd == '&') 5504 which_pat = RE_SUBST; /* use previous substitute pattern */ 5505 else 5506 which_pat = RE_SEARCH; /* use previous search pattern */ 5507 ++cmd; 5508 pat = (char_u *)""; 5509 } 5510 else if (*cmd == NUL) 5511 { 5512 EMSG(_("E148: Regular expression missing from global")); 5513 return; 5514 } 5515 else 5516 { 5517 delim = *cmd; /* get the delimiter */ 5518 if (delim) 5519 ++cmd; /* skip delimiter if there is one */ 5520 pat = cmd; /* remember start of pattern */ 5521 cmd = skip_regexp(cmd, delim, p_magic, &eap->arg); 5522 if (cmd[0] == delim) /* end delimiter found */ 5523 *cmd++ = NUL; /* replace it with a NUL */ 5524 } 5525 5526 #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */ 5527 if (p_altkeymap && curwin->w_p_rl) 5528 lrFswap(pat,0); 5529 #endif 5530 5531 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) 5532 { 5533 EMSG(_(e_invcmd)); 5534 return; 5535 } 5536 5537 /* 5538 * pass 1: set marks for each (not) matching line 5539 */ 5540 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) 5541 { 5542 /* a match on this line? */ 5543 match = vim_regexec_multi(®match, curwin, curbuf, lnum, 5544 (colnr_T)0, NULL); 5545 if ((type == 'g' && match) || (type == 'v' && !match)) 5546 { 5547 ml_setmarked(lnum); 5548 ndone++; 5549 } 5550 line_breakcheck(); 5551 } 5552 5553 /* 5554 * pass 2: execute the command for each line that has been marked 5555 */ 5556 if (got_int) 5557 MSG(_(e_interr)); 5558 else if (ndone == 0) 5559 { 5560 if (type == 'v') 5561 smsg((char_u *)_("Pattern found in every line: %s"), pat); 5562 else 5563 smsg((char_u *)_("Pattern not found: %s"), pat); 5564 } 5565 else 5566 { 5567 #ifdef FEAT_CLIPBOARD 5568 start_global_changes(); 5569 #endif 5570 global_exe(cmd); 5571 #ifdef FEAT_CLIPBOARD 5572 end_global_changes(); 5573 #endif 5574 } 5575 5576 ml_clearmarked(); /* clear rest of the marks */ 5577 vim_regfree(regmatch.regprog); 5578 } 5579 5580 /* 5581 * Execute "cmd" on lines marked with ml_setmarked(). 5582 */ 5583 void 5584 global_exe(char_u *cmd) 5585 { 5586 linenr_T old_lcount; /* b_ml.ml_line_count before the command */ 5587 buf_T *old_buf = curbuf; /* remember what buffer we started in */ 5588 linenr_T lnum; /* line number according to old situation */ 5589 5590 /* 5591 * Set current position only once for a global command. 5592 * If global_busy is set, setpcmark() will not do anything. 5593 * If there is an error, global_busy will be incremented. 5594 */ 5595 setpcmark(); 5596 5597 /* When the command writes a message, don't overwrite the command. */ 5598 msg_didout = TRUE; 5599 5600 sub_nsubs = 0; 5601 sub_nlines = 0; 5602 global_need_beginline = FALSE; 5603 global_busy = 1; 5604 old_lcount = curbuf->b_ml.ml_line_count; 5605 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) 5606 { 5607 curwin->w_cursor.lnum = lnum; 5608 curwin->w_cursor.col = 0; 5609 if (*cmd == NUL || *cmd == '\n') 5610 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); 5611 else 5612 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); 5613 ui_breakcheck(); 5614 } 5615 5616 global_busy = 0; 5617 if (global_need_beginline) 5618 beginline(BL_WHITE | BL_FIX); 5619 else 5620 check_cursor(); /* cursor may be beyond the end of the line */ 5621 5622 /* the cursor may not have moved in the text but a change in a previous 5623 * line may move it on the screen */ 5624 changed_line_abv_curs(); 5625 5626 /* If it looks like no message was written, allow overwriting the 5627 * command with the report for number of changes. */ 5628 if (msg_col == 0 && msg_scrolled == 0) 5629 msg_didout = FALSE; 5630 5631 /* If substitutes done, report number of substitutes, otherwise report 5632 * number of extra or deleted lines. 5633 * Don't report extra or deleted lines in the edge case where the buffer 5634 * we are in after execution is different from the buffer we started in. */ 5635 if (!do_sub_msg(FALSE) && curbuf == old_buf) 5636 msgmore(curbuf->b_ml.ml_line_count - old_lcount); 5637 } 5638 5639 #ifdef FEAT_VIMINFO 5640 int 5641 read_viminfo_sub_string(vir_T *virp, int force) 5642 { 5643 if (force) 5644 vim_free(old_sub); 5645 if (force || old_sub == NULL) 5646 old_sub = viminfo_readstring(virp, 1, TRUE); 5647 return viminfo_readline(virp); 5648 } 5649 5650 void 5651 write_viminfo_sub_string(FILE *fp) 5652 { 5653 if (get_viminfo_parameter('/') != 0 && old_sub != NULL) 5654 { 5655 fputs(_("\n# Last Substitute String:\n$"), fp); 5656 viminfo_writestring(fp, old_sub); 5657 } 5658 } 5659 #endif /* FEAT_VIMINFO */ 5660 5661 #if defined(EXITFREE) || defined(PROTO) 5662 void 5663 free_old_sub(void) 5664 { 5665 vim_free(old_sub); 5666 } 5667 #endif 5668 5669 #if (defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)) || defined(PROTO) 5670 /* 5671 * Set up for a tagpreview. 5672 * Return TRUE when it was created. 5673 */ 5674 int 5675 prepare_tagpreview( 5676 int undo_sync) /* sync undo when leaving the window */ 5677 { 5678 win_T *wp; 5679 5680 # ifdef FEAT_GUI 5681 need_mouse_correct = TRUE; 5682 # endif 5683 5684 /* 5685 * If there is already a preview window open, use that one. 5686 */ 5687 if (!curwin->w_p_pvw) 5688 { 5689 for (wp = firstwin; wp != NULL; wp = wp->w_next) 5690 if (wp->w_p_pvw) 5691 break; 5692 if (wp != NULL) 5693 win_enter(wp, undo_sync); 5694 else 5695 { 5696 /* 5697 * There is no preview window open yet. Create one. 5698 */ 5699 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) 5700 == FAIL) 5701 return FALSE; 5702 curwin->w_p_pvw = TRUE; 5703 curwin->w_p_wfh = TRUE; 5704 RESET_BINDING(curwin); /* don't take over 'scrollbind' 5705 and 'cursorbind' */ 5706 # ifdef FEAT_DIFF 5707 curwin->w_p_diff = FALSE; /* no 'diff' */ 5708 # endif 5709 # ifdef FEAT_FOLDING 5710 curwin->w_p_fdc = 0; /* no 'foldcolumn' */ 5711 # endif 5712 return TRUE; 5713 } 5714 } 5715 return FALSE; 5716 } 5717 5718 #endif 5719 5720 5721 /* 5722 * ":help": open a read-only window on a help file 5723 */ 5724 void 5725 ex_help(exarg_T *eap) 5726 { 5727 char_u *arg; 5728 char_u *tag; 5729 FILE *helpfd; /* file descriptor of help file */ 5730 int n; 5731 int i; 5732 #ifdef FEAT_WINDOWS 5733 win_T *wp; 5734 #endif 5735 int num_matches; 5736 char_u **matches; 5737 char_u *p; 5738 int empty_fnum = 0; 5739 int alt_fnum = 0; 5740 buf_T *buf; 5741 #ifdef FEAT_MULTI_LANG 5742 int len; 5743 char_u *lang; 5744 #endif 5745 #ifdef FEAT_FOLDING 5746 int old_KeyTyped = KeyTyped; 5747 #endif 5748 5749 if (eap != NULL) 5750 { 5751 /* 5752 * A ":help" command ends at the first LF, or at a '|' that is 5753 * followed by some text. Set nextcmd to the following command. 5754 */ 5755 for (arg = eap->arg; *arg; ++arg) 5756 { 5757 if (*arg == '\n' || *arg == '\r' 5758 || (*arg == '|' && arg[1] != NUL && arg[1] != '|')) 5759 { 5760 *arg++ = NUL; 5761 eap->nextcmd = arg; 5762 break; 5763 } 5764 } 5765 arg = eap->arg; 5766 5767 if (eap->forceit && *arg == NUL && !curbuf->b_help) 5768 { 5769 EMSG(_("E478: Don't panic!")); 5770 return; 5771 } 5772 5773 if (eap->skip) /* not executing commands */ 5774 return; 5775 } 5776 else 5777 arg = (char_u *)""; 5778 5779 /* remove trailing blanks */ 5780 p = arg + STRLEN(arg) - 1; 5781 while (p > arg && vim_iswhite(*p) && p[-1] != '\\') 5782 *p-- = NUL; 5783 5784 #ifdef FEAT_MULTI_LANG 5785 /* Check for a specified language */ 5786 lang = check_help_lang(arg); 5787 #endif 5788 5789 /* When no argument given go to the index. */ 5790 if (*arg == NUL) 5791 arg = (char_u *)"help.txt"; 5792 5793 /* 5794 * Check if there is a match for the argument. 5795 */ 5796 n = find_help_tags(arg, &num_matches, &matches, 5797 eap != NULL && eap->forceit); 5798 5799 i = 0; 5800 #ifdef FEAT_MULTI_LANG 5801 if (n != FAIL && lang != NULL) 5802 /* Find first item with the requested language. */ 5803 for (i = 0; i < num_matches; ++i) 5804 { 5805 len = (int)STRLEN(matches[i]); 5806 if (len > 3 && matches[i][len - 3] == '@' 5807 && STRICMP(matches[i] + len - 2, lang) == 0) 5808 break; 5809 } 5810 #endif 5811 if (i >= num_matches || n == FAIL) 5812 { 5813 #ifdef FEAT_MULTI_LANG 5814 if (lang != NULL) 5815 EMSG3(_("E661: Sorry, no '%s' help for %s"), lang, arg); 5816 else 5817 #endif 5818 EMSG2(_("E149: Sorry, no help for %s"), arg); 5819 if (n != FAIL) 5820 FreeWild(num_matches, matches); 5821 return; 5822 } 5823 5824 /* The first match (in the requested language) is the best match. */ 5825 tag = vim_strsave(matches[i]); 5826 FreeWild(num_matches, matches); 5827 5828 #ifdef FEAT_GUI 5829 need_mouse_correct = TRUE; 5830 #endif 5831 5832 /* 5833 * Re-use an existing help window or open a new one. 5834 * Always open a new one for ":tab help". 5835 */ 5836 if (!curwin->w_buffer->b_help 5837 #ifdef FEAT_WINDOWS 5838 || cmdmod.tab != 0 5839 #endif 5840 ) 5841 { 5842 #ifdef FEAT_WINDOWS 5843 if (cmdmod.tab != 0) 5844 wp = NULL; 5845 else 5846 for (wp = firstwin; wp != NULL; wp = wp->w_next) 5847 if (wp->w_buffer != NULL && wp->w_buffer->b_help) 5848 break; 5849 if (wp != NULL && wp->w_buffer->b_nwindows > 0) 5850 win_enter(wp, TRUE); 5851 else 5852 #endif 5853 { 5854 /* 5855 * There is no help window yet. 5856 * Try to open the file specified by the "helpfile" option. 5857 */ 5858 if ((helpfd = mch_fopen((char *)p_hf, READBIN)) == NULL) 5859 { 5860 smsg((char_u *)_("Sorry, help file \"%s\" not found"), p_hf); 5861 goto erret; 5862 } 5863 fclose(helpfd); 5864 5865 #ifdef FEAT_WINDOWS 5866 /* Split off help window; put it at far top if no position 5867 * specified, the current window is vertically split and 5868 * narrow. */ 5869 n = WSP_HELP; 5870 if (cmdmod.split == 0 && curwin->w_width != Columns 5871 && curwin->w_width < 80) 5872 n |= WSP_TOP; 5873 if (win_split(0, n) == FAIL) 5874 goto erret; 5875 #else 5876 /* use current window */ 5877 if (!can_abandon(curbuf, FALSE)) 5878 goto erret; 5879 #endif 5880 5881 #ifdef FEAT_WINDOWS 5882 if (curwin->w_height < p_hh) 5883 win_setheight((int)p_hh); 5884 #endif 5885 5886 /* 5887 * Open help file (do_ecmd() will set b_help flag, readfile() will 5888 * set b_p_ro flag). 5889 * Set the alternate file to the previously edited file. 5890 */ 5891 alt_fnum = curbuf->b_fnum; 5892 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_LASTL, 5893 ECMD_HIDE + ECMD_SET_HELP, 5894 #ifdef FEAT_WINDOWS 5895 NULL /* buffer is still open, don't store info */ 5896 #else 5897 curwin 5898 #endif 5899 ); 5900 if (!cmdmod.keepalt) 5901 curwin->w_alt_fnum = alt_fnum; 5902 empty_fnum = curbuf->b_fnum; 5903 } 5904 } 5905 5906 if (!p_im) 5907 restart_edit = 0; /* don't want insert mode in help file */ 5908 5909 #ifdef FEAT_FOLDING 5910 /* Restore KeyTyped, setting 'filetype=help' may reset it. 5911 * It is needed for do_tag top open folds under the cursor. */ 5912 KeyTyped = old_KeyTyped; 5913 #endif 5914 5915 if (tag != NULL) 5916 do_tag(tag, DT_HELP, 1, FALSE, TRUE); 5917 5918 /* Delete the empty buffer if we're not using it. Careful: autocommands 5919 * may have jumped to another window, check that the buffer is not in a 5920 * window. */ 5921 if (empty_fnum != 0 && curbuf->b_fnum != empty_fnum) 5922 { 5923 buf = buflist_findnr(empty_fnum); 5924 if (buf != NULL && buf->b_nwindows == 0) 5925 wipe_buffer(buf, TRUE); 5926 } 5927 5928 /* keep the previous alternate file */ 5929 if (alt_fnum != 0 && curwin->w_alt_fnum == empty_fnum && !cmdmod.keepalt) 5930 curwin->w_alt_fnum = alt_fnum; 5931 5932 erret: 5933 vim_free(tag); 5934 } 5935 5936 /* 5937 * ":helpclose": Close one help window 5938 */ 5939 void 5940 ex_helpclose(exarg_T *eap UNUSED) 5941 { 5942 #if defined(FEAT_WINDOWS) 5943 win_T *win; 5944 5945 FOR_ALL_WINDOWS(win) 5946 { 5947 if (win->w_buffer->b_help) 5948 { 5949 win_close(win, FALSE); 5950 return; 5951 } 5952 } 5953 #endif 5954 } 5955 5956 #if defined(FEAT_MULTI_LANG) || defined(PROTO) 5957 /* 5958 * In an argument search for a language specifiers in the form "@xx". 5959 * Changes the "@" to NUL if found, and returns a pointer to "xx". 5960 * Returns NULL if not found. 5961 */ 5962 char_u * 5963 check_help_lang(char_u *arg) 5964 { 5965 int len = (int)STRLEN(arg); 5966 5967 if (len >= 3 && arg[len - 3] == '@' && ASCII_ISALPHA(arg[len - 2]) 5968 && ASCII_ISALPHA(arg[len - 1])) 5969 { 5970 arg[len - 3] = NUL; /* remove the '@' */ 5971 return arg + len - 2; 5972 } 5973 return NULL; 5974 } 5975 #endif 5976 5977 /* 5978 * Return a heuristic indicating how well the given string matches. The 5979 * smaller the number, the better the match. This is the order of priorities, 5980 * from best match to worst match: 5981 * - Match with least alpha-numeric characters is better. 5982 * - Match with least total characters is better. 5983 * - Match towards the start is better. 5984 * - Match starting with "+" is worse (feature instead of command) 5985 * Assumption is made that the matched_string passed has already been found to 5986 * match some string for which help is requested. webb. 5987 */ 5988 int 5989 help_heuristic( 5990 char_u *matched_string, 5991 int offset, /* offset for match */ 5992 int wrong_case) /* no matching case */ 5993 { 5994 int num_letters; 5995 char_u *p; 5996 5997 num_letters = 0; 5998 for (p = matched_string; *p; p++) 5999 if (ASCII_ISALNUM(*p)) 6000 num_letters++; 6001 6002 /* 6003 * Multiply the number of letters by 100 to give it a much bigger 6004 * weighting than the number of characters. 6005 * If there only is a match while ignoring case, add 5000. 6006 * If the match starts in the middle of a word, add 10000 to put it 6007 * somewhere in the last half. 6008 * If the match is more than 2 chars from the start, multiply by 200 to 6009 * put it after matches at the start. 6010 */ 6011 if (ASCII_ISALNUM(matched_string[offset]) && offset > 0 6012 && ASCII_ISALNUM(matched_string[offset - 1])) 6013 offset += 10000; 6014 else if (offset > 2) 6015 offset *= 200; 6016 if (wrong_case) 6017 offset += 5000; 6018 /* Features are less interesting than the subjects themselves, but "+" 6019 * alone is not a feature. */ 6020 if (matched_string[0] == '+' && matched_string[1] != NUL) 6021 offset += 100; 6022 return (int)(100 * num_letters + STRLEN(matched_string) + offset); 6023 } 6024 6025 /* 6026 * Compare functions for qsort() below, that checks the help heuristics number 6027 * that has been put after the tagname by find_tags(). 6028 */ 6029 static int 6030 #ifdef __BORLANDC__ 6031 _RTLENTRYF 6032 #endif 6033 help_compare(const void *s1, const void *s2) 6034 { 6035 char *p1; 6036 char *p2; 6037 6038 p1 = *(char **)s1 + strlen(*(char **)s1) + 1; 6039 p2 = *(char **)s2 + strlen(*(char **)s2) + 1; 6040 return strcmp(p1, p2); 6041 } 6042 6043 /* 6044 * Find all help tags matching "arg", sort them and return in matches[], with 6045 * the number of matches in num_matches. 6046 * The matches will be sorted with a "best" match algorithm. 6047 * When "keep_lang" is TRUE try keeping the language of the current buffer. 6048 */ 6049 int 6050 find_help_tags( 6051 char_u *arg, 6052 int *num_matches, 6053 char_u ***matches, 6054 int keep_lang) 6055 { 6056 char_u *s, *d; 6057 int i; 6058 static char *(mtable[]) = {"*", "g*", "[*", "]*", ":*", 6059 "/*", "/\\*", "\"*", "**", 6060 "cpo-*", "/\\(\\)", "/\\%(\\)", 6061 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", 6062 "/\\?", "/\\z(\\)", "\\=", ":s\\=", 6063 "[count]", "[quotex]", "[range]", 6064 "[pattern]", "\\|", "\\%$", 6065 "s/\\~", "s/\\U", "s/\\L", 6066 "s/\\1", "s/\\2", "s/\\3", "s/\\9"}; 6067 static char *(rtable[]) = {"star", "gstar", "[star", "]star", ":star", 6068 "/star", "/\\\\star", "quotestar", "starstar", 6069 "cpo-star", "/\\\\(\\\\)", "/\\\\%(\\\\)", 6070 "?", ":?", "?<CR>", "g?", "g?g?", "g??", "z?", 6071 "/\\\\?", "/\\\\z(\\\\)", "\\\\=", ":s\\\\=", 6072 "\\[count]", "\\[quotex]", "\\[range]", 6073 "\\[pattern]", "\\\\bar", "/\\\\%\\$", 6074 "s/\\\\\\~", "s/\\\\U", "s/\\\\L", 6075 "s/\\\\1", "s/\\\\2", "s/\\\\3", "s/\\\\9"}; 6076 int flags; 6077 6078 d = IObuff; /* assume IObuff is long enough! */ 6079 6080 /* 6081 * Recognize a few exceptions to the rule. Some strings that contain '*' 6082 * with "star". Otherwise '*' is recognized as a wildcard. 6083 */ 6084 for (i = (int)(sizeof(mtable) / sizeof(char *)); --i >= 0; ) 6085 if (STRCMP(arg, mtable[i]) == 0) 6086 { 6087 STRCPY(d, rtable[i]); 6088 break; 6089 } 6090 6091 if (i < 0) /* no match in table */ 6092 { 6093 /* Replace "\S" with "/\\S", etc. Otherwise every tag is matched. 6094 * Also replace "\%^" and "\%(", they match every tag too. 6095 * Also "\zs", "\z1", etc. 6096 * Also "\@<", "\@=", "\@<=", etc. 6097 * And also "\_$" and "\_^". */ 6098 if (arg[0] == '\\' 6099 && ((arg[1] != NUL && arg[2] == NUL) 6100 || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL 6101 && arg[2] != NUL))) 6102 { 6103 STRCPY(d, "/\\\\"); 6104 STRCPY(d + 3, arg + 1); 6105 /* Check for "/\\_$", should be "/\\_\$" */ 6106 if (d[3] == '_' && d[4] == '$') 6107 STRCPY(d + 4, "\\$"); 6108 } 6109 else 6110 { 6111 /* Replace: 6112 * "[:...:]" with "\[:...:]" 6113 * "[++...]" with "\[++...]" 6114 * "\{" with "\\{" -- matching "} \}" 6115 */ 6116 if ((arg[0] == '[' && (arg[1] == ':' 6117 || (arg[1] == '+' && arg[2] == '+'))) 6118 || (arg[0] == '\\' && arg[1] == '{')) 6119 *d++ = '\\'; 6120 6121 /* 6122 * If tag starts with "('", skip the "(". Fixes CTRL-] on ('option'. 6123 */ 6124 if (*arg == '(' && arg[1] == '\'') 6125 arg++; 6126 for (s = arg; *s; ++s) 6127 { 6128 /* 6129 * Replace "|" with "bar" and '"' with "quote" to match the name of 6130 * the tags for these commands. 6131 * Replace "*" with ".*" and "?" with "." to match command line 6132 * completion. 6133 * Insert a backslash before '~', '$' and '.' to avoid their 6134 * special meaning. 6135 */ 6136 if (d - IObuff > IOSIZE - 10) /* getting too long!? */ 6137 break; 6138 switch (*s) 6139 { 6140 case '|': STRCPY(d, "bar"); 6141 d += 3; 6142 continue; 6143 case '"': STRCPY(d, "quote"); 6144 d += 5; 6145 continue; 6146 case '*': *d++ = '.'; 6147 break; 6148 case '?': *d++ = '.'; 6149 continue; 6150 case '$': 6151 case '.': 6152 case '~': *d++ = '\\'; 6153 break; 6154 } 6155 6156 /* 6157 * Replace "^x" by "CTRL-X". Don't do this for "^_" to make 6158 * ":help i_^_CTRL-D" work. 6159 * Insert '-' before and after "CTRL-X" when applicable. 6160 */ 6161 if (*s < ' ' || (*s == '^' && s[1] && (ASCII_ISALPHA(s[1]) 6162 || vim_strchr((char_u *)"?@[\\]^", s[1]) != NULL))) 6163 { 6164 if (d > IObuff && d[-1] != '_' && d[-1] != '\\') 6165 *d++ = '_'; /* prepend a '_' to make x_CTRL-x */ 6166 STRCPY(d, "CTRL-"); 6167 d += 5; 6168 if (*s < ' ') 6169 { 6170 #ifdef EBCDIC 6171 *d++ = CtrlChar(*s); 6172 #else 6173 *d++ = *s + '@'; 6174 #endif 6175 if (d[-1] == '\\') 6176 *d++ = '\\'; /* double a backslash */ 6177 } 6178 else 6179 *d++ = *++s; 6180 if (s[1] != NUL && s[1] != '_') 6181 *d++ = '_'; /* append a '_' */ 6182 continue; 6183 } 6184 else if (*s == '^') /* "^" or "CTRL-^" or "^_" */ 6185 *d++ = '\\'; 6186 6187 /* 6188 * Insert a backslash before a backslash after a slash, for search 6189 * pattern tags: "/\|" --> "/\\|". 6190 */ 6191 else if (s[0] == '\\' && s[1] != '\\' 6192 && *arg == '/' && s == arg + 1) 6193 *d++ = '\\'; 6194 6195 /* "CTRL-\_" -> "CTRL-\\_" to avoid the special meaning of "\_" in 6196 * "CTRL-\_CTRL-N" */ 6197 if (STRNICMP(s, "CTRL-\\_", 7) == 0) 6198 { 6199 STRCPY(d, "CTRL-\\\\"); 6200 d += 7; 6201 s += 6; 6202 } 6203 6204 *d++ = *s; 6205 6206 /* 6207 * If tag starts with ', toss everything after a second '. Fixes 6208 * CTRL-] on 'option'. (would include the trailing '.'). 6209 */ 6210 if (*s == '\'' && s > arg && *arg == '\'') 6211 break; 6212 } 6213 *d = NUL; 6214 6215 if (*IObuff == '`') 6216 { 6217 if (d > IObuff + 2 && d[-1] == '`') 6218 { 6219 /* remove the backticks from `command` */ 6220 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); 6221 d[-2] = NUL; 6222 } 6223 else if (d > IObuff + 3 && d[-2] == '`' && d[-1] == ',') 6224 { 6225 /* remove the backticks and comma from `command`, */ 6226 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); 6227 d[-3] = NUL; 6228 } 6229 else if (d > IObuff + 4 && d[-3] == '`' 6230 && d[-2] == '\\' && d[-1] == '.') 6231 { 6232 /* remove the backticks and dot from `command`\. */ 6233 mch_memmove(IObuff, IObuff + 1, STRLEN(IObuff)); 6234 d[-4] = NUL; 6235 } 6236 } 6237 } 6238 } 6239 6240 *matches = (char_u **)""; 6241 *num_matches = 0; 6242 flags = TAG_HELP | TAG_REGEXP | TAG_NAMES | TAG_VERBOSE; 6243 if (keep_lang) 6244 flags |= TAG_KEEP_LANG; 6245 if (find_tags(IObuff, num_matches, matches, flags, (int)MAXCOL, NULL) == OK 6246 && *num_matches > 0) 6247 { 6248 /* Sort the matches found on the heuristic number that is after the 6249 * tag name. */ 6250 qsort((void *)*matches, (size_t)*num_matches, 6251 sizeof(char_u *), help_compare); 6252 /* Delete more than TAG_MANY to reduce the size of the listing. */ 6253 while (*num_matches > TAG_MANY) 6254 vim_free((*matches)[--*num_matches]); 6255 } 6256 return OK; 6257 } 6258 6259 /* 6260 * Called when starting to edit a buffer for a help file. 6261 */ 6262 static void 6263 prepare_help_buffer(void) 6264 { 6265 char_u *p; 6266 6267 curbuf->b_help = TRUE; 6268 #ifdef FEAT_QUICKFIX 6269 set_string_option_direct((char_u *)"buftype", -1, 6270 (char_u *)"help", OPT_FREE|OPT_LOCAL, 0); 6271 #endif 6272 6273 /* 6274 * Always set these options after jumping to a help tag, because the 6275 * user may have an autocommand that gets in the way. 6276 * Accept all ASCII chars for keywords, except ' ', '*', '"', '|', and 6277 * latin1 word characters (for translated help files). 6278 * Only set it when needed, buf_init_chartab() is some work. 6279 */ 6280 p = 6281 #ifdef EBCDIC 6282 (char_u *)"65-255,^*,^|,^\""; 6283 #else 6284 (char_u *)"!-~,^*,^|,^\",192-255"; 6285 #endif 6286 if (STRCMP(curbuf->b_p_isk, p) != 0) 6287 { 6288 set_string_option_direct((char_u *)"isk", -1, p, OPT_FREE|OPT_LOCAL, 0); 6289 check_buf_options(curbuf); 6290 (void)buf_init_chartab(curbuf, FALSE); 6291 } 6292 6293 #ifdef FEAT_FOLDING 6294 /* Don't use the global foldmethod.*/ 6295 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"manual", 6296 OPT_FREE|OPT_LOCAL, 0); 6297 #endif 6298 6299 curbuf->b_p_ts = 8; /* 'tabstop' is 8 */ 6300 curwin->w_p_list = FALSE; /* no list mode */ 6301 6302 curbuf->b_p_ma = FALSE; /* not modifiable */ 6303 curbuf->b_p_bin = FALSE; /* reset 'bin' before reading file */ 6304 curwin->w_p_nu = 0; /* no line numbers */ 6305 curwin->w_p_rnu = 0; /* no relative line numbers */ 6306 RESET_BINDING(curwin); /* no scroll or cursor binding */ 6307 #ifdef FEAT_ARABIC 6308 curwin->w_p_arab = FALSE; /* no arabic mode */ 6309 #endif 6310 #ifdef FEAT_RIGHTLEFT 6311 curwin->w_p_rl = FALSE; /* help window is left-to-right */ 6312 #endif 6313 #ifdef FEAT_FOLDING 6314 curwin->w_p_fen = FALSE; /* No folding in the help window */ 6315 #endif 6316 #ifdef FEAT_DIFF 6317 curwin->w_p_diff = FALSE; /* No 'diff' */ 6318 #endif 6319 #ifdef FEAT_SPELL 6320 curwin->w_p_spell = FALSE; /* No spell checking */ 6321 #endif 6322 6323 set_buflisted(FALSE); 6324 } 6325 6326 /* 6327 * After reading a help file: May cleanup a help buffer when syntax 6328 * highlighting is not used. 6329 */ 6330 void 6331 fix_help_buffer(void) 6332 { 6333 linenr_T lnum; 6334 char_u *line; 6335 int in_example = FALSE; 6336 int len; 6337 char_u *fname; 6338 char_u *p; 6339 char_u *rt; 6340 int mustfree; 6341 6342 /* set filetype to "help". */ 6343 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); 6344 6345 #ifdef FEAT_SYN_HL 6346 if (!syntax_present(curwin)) 6347 #endif 6348 { 6349 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) 6350 { 6351 line = ml_get_buf(curbuf, lnum, FALSE); 6352 len = (int)STRLEN(line); 6353 if (in_example && len > 0 && !vim_iswhite(line[0])) 6354 { 6355 /* End of example: non-white or '<' in first column. */ 6356 if (line[0] == '<') 6357 { 6358 /* blank-out a '<' in the first column */ 6359 line = ml_get_buf(curbuf, lnum, TRUE); 6360 line[0] = ' '; 6361 } 6362 in_example = FALSE; 6363 } 6364 if (!in_example && len > 0) 6365 { 6366 if (line[len - 1] == '>' && (len == 1 || line[len - 2] == ' ')) 6367 { 6368 /* blank-out a '>' in the last column (start of example) */ 6369 line = ml_get_buf(curbuf, lnum, TRUE); 6370 line[len - 1] = ' '; 6371 in_example = TRUE; 6372 } 6373 else if (line[len - 1] == '~') 6374 { 6375 /* blank-out a '~' at the end of line (header marker) */ 6376 line = ml_get_buf(curbuf, lnum, TRUE); 6377 line[len - 1] = ' '; 6378 } 6379 } 6380 } 6381 } 6382 6383 /* 6384 * In the "help.txt" and "help.abx" file, add the locally added help 6385 * files. This uses the very first line in the help file. 6386 */ 6387 fname = gettail(curbuf->b_fname); 6388 if (fnamecmp(fname, "help.txt") == 0 6389 #ifdef FEAT_MULTI_LANG 6390 || (fnamencmp(fname, "help.", 5) == 0 6391 && ASCII_ISALPHA(fname[5]) 6392 && ASCII_ISALPHA(fname[6]) 6393 && TOLOWER_ASC(fname[7]) == 'x' 6394 && fname[8] == NUL) 6395 #endif 6396 ) 6397 { 6398 for (lnum = 1; lnum < curbuf->b_ml.ml_line_count; ++lnum) 6399 { 6400 line = ml_get_buf(curbuf, lnum, FALSE); 6401 if (strstr((char *)line, "*local-additions*") == NULL) 6402 continue; 6403 6404 /* Go through all directories in 'runtimepath', skipping 6405 * $VIMRUNTIME. */ 6406 p = p_rtp; 6407 while (*p != NUL) 6408 { 6409 copy_option_part(&p, NameBuff, MAXPATHL, ","); 6410 mustfree = FALSE; 6411 rt = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); 6412 if (fullpathcmp(rt, NameBuff, FALSE) != FPC_SAME) 6413 { 6414 int fcount; 6415 char_u **fnames; 6416 FILE *fd; 6417 char_u *s; 6418 int fi; 6419 #ifdef FEAT_MBYTE 6420 vimconv_T vc; 6421 char_u *cp; 6422 #endif 6423 6424 /* Find all "doc/ *.txt" files in this directory. */ 6425 add_pathsep(NameBuff); 6426 #ifdef FEAT_MULTI_LANG 6427 STRCAT(NameBuff, "doc/*.??[tx]"); 6428 #else 6429 STRCAT(NameBuff, "doc/*.txt"); 6430 #endif 6431 if (gen_expand_wildcards(1, &NameBuff, &fcount, 6432 &fnames, EW_FILE|EW_SILENT) == OK 6433 && fcount > 0) 6434 { 6435 #ifdef FEAT_MULTI_LANG 6436 int i1; 6437 int i2; 6438 char_u *f1; 6439 char_u *f2; 6440 char_u *t1; 6441 char_u *e1; 6442 char_u *e2; 6443 6444 /* If foo.abx is found use it instead of foo.txt in 6445 * the same directory. */ 6446 for (i1 = 0; i1 < fcount; ++i1) 6447 { 6448 for (i2 = 0; i2 < fcount; ++i2) 6449 { 6450 if (i1 == i2) 6451 continue; 6452 if (fnames[i1] == NULL || fnames[i2] == NULL) 6453 continue; 6454 f1 = fnames[i1]; 6455 f2 = fnames[i2]; 6456 t1 = gettail(f1); 6457 if (fnamencmp(f1, f2, t1 - f1) != 0) 6458 continue; 6459 e1 = vim_strrchr(t1, '.'); 6460 e2 = vim_strrchr(gettail(f2), '.'); 6461 if (e1 == NUL || e2 == NUL) 6462 continue; 6463 if (fnamecmp(e1, ".txt") != 0 6464 && fnamecmp(e1, fname + 4) != 0) 6465 { 6466 /* Not .txt and not .abx, remove it. */ 6467 vim_free(fnames[i1]); 6468 fnames[i1] = NULL; 6469 continue; 6470 } 6471 if (fnamencmp(f1, f2, e1 - f1) != 0) 6472 continue; 6473 if (fnamecmp(e1, ".txt") == 0 6474 && fnamecmp(e2, fname + 4) == 0) 6475 { 6476 /* use .abx instead of .txt */ 6477 vim_free(fnames[i1]); 6478 fnames[i1] = NULL; 6479 } 6480 } 6481 } 6482 #endif 6483 for (fi = 0; fi < fcount; ++fi) 6484 { 6485 if (fnames[fi] == NULL) 6486 continue; 6487 fd = mch_fopen((char *)fnames[fi], "r"); 6488 if (fd != NULL) 6489 { 6490 vim_fgets(IObuff, IOSIZE, fd); 6491 if (IObuff[0] == '*' 6492 && (s = vim_strchr(IObuff + 1, '*')) 6493 != NULL) 6494 { 6495 #ifdef FEAT_MBYTE 6496 int this_utf = MAYBE; 6497 #endif 6498 /* Change tag definition to a 6499 * reference and remove <CR>/<NL>. */ 6500 IObuff[0] = '|'; 6501 *s = '|'; 6502 while (*s != NUL) 6503 { 6504 if (*s == '\r' || *s == '\n') 6505 *s = NUL; 6506 #ifdef FEAT_MBYTE 6507 /* The text is utf-8 when a byte 6508 * above 127 is found and no 6509 * illegal byte sequence is found. 6510 */ 6511 if (*s >= 0x80 && this_utf != FALSE) 6512 { 6513 int l; 6514 6515 this_utf = TRUE; 6516 l = utf_ptr2len(s); 6517 if (l == 1) 6518 this_utf = FALSE; 6519 s += l - 1; 6520 } 6521 #endif 6522 ++s; 6523 } 6524 #ifdef FEAT_MBYTE 6525 /* The help file is latin1 or utf-8; 6526 * conversion to the current 6527 * 'encoding' may be required. */ 6528 vc.vc_type = CONV_NONE; 6529 convert_setup(&vc, (char_u *)( 6530 this_utf == TRUE ? "utf-8" 6531 : "latin1"), p_enc); 6532 if (vc.vc_type == CONV_NONE) 6533 /* No conversion needed. */ 6534 cp = IObuff; 6535 else 6536 { 6537 /* Do the conversion. If it fails 6538 * use the unconverted text. */ 6539 cp = string_convert(&vc, IObuff, 6540 NULL); 6541 if (cp == NULL) 6542 cp = IObuff; 6543 } 6544 convert_setup(&vc, NULL, NULL); 6545 6546 ml_append(lnum, cp, (colnr_T)0, FALSE); 6547 if (cp != IObuff) 6548 vim_free(cp); 6549 #else 6550 ml_append(lnum, IObuff, (colnr_T)0, 6551 FALSE); 6552 #endif 6553 ++lnum; 6554 } 6555 fclose(fd); 6556 } 6557 } 6558 FreeWild(fcount, fnames); 6559 } 6560 } 6561 if (mustfree) 6562 vim_free(rt); 6563 } 6564 break; 6565 } 6566 } 6567 } 6568 6569 /* 6570 * ":exusage" 6571 */ 6572 void 6573 ex_exusage(exarg_T *eap UNUSED) 6574 { 6575 do_cmdline_cmd((char_u *)"help ex-cmd-index"); 6576 } 6577 6578 /* 6579 * ":viusage" 6580 */ 6581 void 6582 ex_viusage(exarg_T *eap UNUSED) 6583 { 6584 do_cmdline_cmd((char_u *)"help normal-index"); 6585 } 6586 6587 /* 6588 * Generate tags in one help directory. 6589 */ 6590 static void 6591 helptags_one( 6592 char_u *dir, /* doc directory */ 6593 char_u *ext, /* suffix, ".txt", ".itx", ".frx", etc. */ 6594 char_u *tagfname, /* "tags" for English, "tags-fr" for French. */ 6595 int add_help_tags) /* add "help-tags" tag */ 6596 { 6597 FILE *fd_tags; 6598 FILE *fd; 6599 garray_T ga; 6600 int filecount; 6601 char_u **files; 6602 char_u *p1, *p2; 6603 int fi; 6604 char_u *s; 6605 int i; 6606 char_u *fname; 6607 int dirlen; 6608 # ifdef FEAT_MBYTE 6609 int utf8 = MAYBE; 6610 int this_utf8; 6611 int firstline; 6612 int mix = FALSE; /* detected mixed encodings */ 6613 # endif 6614 6615 /* 6616 * Find all *.txt files. 6617 */ 6618 dirlen = (int)STRLEN(dir); 6619 STRCPY(NameBuff, dir); 6620 STRCAT(NameBuff, "/**/*"); 6621 STRCAT(NameBuff, ext); 6622 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, 6623 EW_FILE|EW_SILENT) == FAIL 6624 || filecount == 0) 6625 { 6626 if (!got_int) 6627 EMSG2("E151: No match: %s", NameBuff); 6628 return; 6629 } 6630 6631 /* 6632 * Open the tags file for writing. 6633 * Do this before scanning through all the files. 6634 */ 6635 STRCPY(NameBuff, dir); 6636 add_pathsep(NameBuff); 6637 STRCAT(NameBuff, tagfname); 6638 fd_tags = mch_fopen((char *)NameBuff, "w"); 6639 if (fd_tags == NULL) 6640 { 6641 EMSG2(_("E152: Cannot open %s for writing"), NameBuff); 6642 FreeWild(filecount, files); 6643 return; 6644 } 6645 6646 /* 6647 * If using the "++t" argument or generating tags for "$VIMRUNTIME/doc" 6648 * add the "help-tags" tag. 6649 */ 6650 ga_init2(&ga, (int)sizeof(char_u *), 100); 6651 if (add_help_tags || fullpathcmp((char_u *)"$VIMRUNTIME/doc", 6652 dir, FALSE) == FPC_SAME) 6653 { 6654 if (ga_grow(&ga, 1) == FAIL) 6655 got_int = TRUE; 6656 else 6657 { 6658 s = alloc(18 + (unsigned)STRLEN(tagfname)); 6659 if (s == NULL) 6660 got_int = TRUE; 6661 else 6662 { 6663 sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); 6664 ((char_u **)ga.ga_data)[ga.ga_len] = s; 6665 ++ga.ga_len; 6666 } 6667 } 6668 } 6669 6670 /* 6671 * Go over all the files and extract the tags. 6672 */ 6673 for (fi = 0; fi < filecount && !got_int; ++fi) 6674 { 6675 fd = mch_fopen((char *)files[fi], "r"); 6676 if (fd == NULL) 6677 { 6678 EMSG2(_("E153: Unable to open %s for reading"), files[fi]); 6679 continue; 6680 } 6681 fname = files[fi] + dirlen + 1; 6682 6683 # ifdef FEAT_MBYTE 6684 firstline = TRUE; 6685 # endif 6686 while (!vim_fgets(IObuff, IOSIZE, fd) && !got_int) 6687 { 6688 # ifdef FEAT_MBYTE 6689 if (firstline) 6690 { 6691 /* Detect utf-8 file by a non-ASCII char in the first line. */ 6692 this_utf8 = MAYBE; 6693 for (s = IObuff; *s != NUL; ++s) 6694 if (*s >= 0x80) 6695 { 6696 int l; 6697 6698 this_utf8 = TRUE; 6699 l = utf_ptr2len(s); 6700 if (l == 1) 6701 { 6702 /* Illegal UTF-8 byte sequence. */ 6703 this_utf8 = FALSE; 6704 break; 6705 } 6706 s += l - 1; 6707 } 6708 if (this_utf8 == MAYBE) /* only ASCII characters found */ 6709 this_utf8 = FALSE; 6710 if (utf8 == MAYBE) /* first file */ 6711 utf8 = this_utf8; 6712 else if (utf8 != this_utf8) 6713 { 6714 EMSG2(_("E670: Mix of help file encodings within a language: %s"), files[fi]); 6715 mix = !got_int; 6716 got_int = TRUE; 6717 } 6718 firstline = FALSE; 6719 } 6720 # endif 6721 p1 = vim_strchr(IObuff, '*'); /* find first '*' */ 6722 while (p1 != NULL) 6723 { 6724 /* Use vim_strbyte() instead of vim_strchr() so that when 6725 * 'encoding' is dbcs it still works, don't find '*' in the 6726 * second byte. */ 6727 p2 = vim_strbyte(p1 + 1, '*'); /* find second '*' */ 6728 if (p2 != NULL && p2 > p1 + 1) /* skip "*" and "**" */ 6729 { 6730 for (s = p1 + 1; s < p2; ++s) 6731 if (*s == ' ' || *s == '\t' || *s == '|') 6732 break; 6733 6734 /* 6735 * Only accept a *tag* when it consists of valid 6736 * characters, there is white space before it and is 6737 * followed by a white character or end-of-line. 6738 */ 6739 if (s == p2 6740 && (p1 == IObuff || p1[-1] == ' ' || p1[-1] == '\t') 6741 && (vim_strchr((char_u *)" \t\n\r", s[1]) != NULL 6742 || s[1] == '\0')) 6743 { 6744 *p2 = '\0'; 6745 ++p1; 6746 if (ga_grow(&ga, 1) == FAIL) 6747 { 6748 got_int = TRUE; 6749 break; 6750 } 6751 s = alloc((unsigned)(p2 - p1 + STRLEN(fname) + 2)); 6752 if (s == NULL) 6753 { 6754 got_int = TRUE; 6755 break; 6756 } 6757 ((char_u **)ga.ga_data)[ga.ga_len] = s; 6758 ++ga.ga_len; 6759 sprintf((char *)s, "%s\t%s", p1, fname); 6760 6761 /* find next '*' */ 6762 p2 = vim_strchr(p2 + 1, '*'); 6763 } 6764 } 6765 p1 = p2; 6766 } 6767 line_breakcheck(); 6768 } 6769 6770 fclose(fd); 6771 } 6772 6773 FreeWild(filecount, files); 6774 6775 if (!got_int) 6776 { 6777 /* 6778 * Sort the tags. 6779 */ 6780 if (ga.ga_data != NULL) 6781 sort_strings((char_u **)ga.ga_data, ga.ga_len); 6782 6783 /* 6784 * Check for duplicates. 6785 */ 6786 for (i = 1; i < ga.ga_len; ++i) 6787 { 6788 p1 = ((char_u **)ga.ga_data)[i - 1]; 6789 p2 = ((char_u **)ga.ga_data)[i]; 6790 while (*p1 == *p2) 6791 { 6792 if (*p2 == '\t') 6793 { 6794 *p2 = NUL; 6795 vim_snprintf((char *)NameBuff, MAXPATHL, 6796 _("E154: Duplicate tag \"%s\" in file %s/%s"), 6797 ((char_u **)ga.ga_data)[i], dir, p2 + 1); 6798 EMSG(NameBuff); 6799 *p2 = '\t'; 6800 break; 6801 } 6802 ++p1; 6803 ++p2; 6804 } 6805 } 6806 6807 # ifdef FEAT_MBYTE 6808 if (utf8 == TRUE) 6809 fprintf(fd_tags, "!_TAG_FILE_ENCODING\tutf-8\t//\n"); 6810 # endif 6811 6812 /* 6813 * Write the tags into the file. 6814 */ 6815 for (i = 0; i < ga.ga_len; ++i) 6816 { 6817 s = ((char_u **)ga.ga_data)[i]; 6818 if (STRNCMP(s, "help-tags\t", 10) == 0) 6819 /* help-tags entry was added in formatted form */ 6820 fputs((char *)s, fd_tags); 6821 else 6822 { 6823 fprintf(fd_tags, "%s\t/*", s); 6824 for (p1 = s; *p1 != '\t'; ++p1) 6825 { 6826 /* insert backslash before '\\' and '/' */ 6827 if (*p1 == '\\' || *p1 == '/') 6828 putc('\\', fd_tags); 6829 putc(*p1, fd_tags); 6830 } 6831 fprintf(fd_tags, "*\n"); 6832 } 6833 } 6834 } 6835 #ifdef FEAT_MBYTE 6836 if (mix) 6837 got_int = FALSE; /* continue with other languages */ 6838 #endif 6839 6840 for (i = 0; i < ga.ga_len; ++i) 6841 vim_free(((char_u **)ga.ga_data)[i]); 6842 ga_clear(&ga); 6843 fclose(fd_tags); /* there is no check for an error... */ 6844 } 6845 6846 /* 6847 * Generate tags in one help directory, taking care of translations. 6848 */ 6849 static void 6850 do_helptags(char_u *dirname, int add_help_tags) 6851 { 6852 #ifdef FEAT_MULTI_LANG 6853 int len; 6854 int i, j; 6855 garray_T ga; 6856 char_u lang[2]; 6857 char_u ext[5]; 6858 char_u fname[8]; 6859 int filecount; 6860 char_u **files; 6861 6862 /* Get a list of all files in the help directory and in subdirectories. */ 6863 STRCPY(NameBuff, dirname); 6864 add_pathsep(NameBuff); 6865 STRCAT(NameBuff, "**"); 6866 if (gen_expand_wildcards(1, &NameBuff, &filecount, &files, 6867 EW_FILE|EW_SILENT) == FAIL 6868 || filecount == 0) 6869 { 6870 EMSG2("E151: No match: %s", NameBuff); 6871 return; 6872 } 6873 6874 /* Go over all files in the directory to find out what languages are 6875 * present. */ 6876 ga_init2(&ga, 1, 10); 6877 for (i = 0; i < filecount; ++i) 6878 { 6879 len = (int)STRLEN(files[i]); 6880 if (len > 4) 6881 { 6882 if (STRICMP(files[i] + len - 4, ".txt") == 0) 6883 { 6884 /* ".txt" -> language "en" */ 6885 lang[0] = 'e'; 6886 lang[1] = 'n'; 6887 } 6888 else if (files[i][len - 4] == '.' 6889 && ASCII_ISALPHA(files[i][len - 3]) 6890 && ASCII_ISALPHA(files[i][len - 2]) 6891 && TOLOWER_ASC(files[i][len - 1]) == 'x') 6892 { 6893 /* ".abx" -> language "ab" */ 6894 lang[0] = TOLOWER_ASC(files[i][len - 3]); 6895 lang[1] = TOLOWER_ASC(files[i][len - 2]); 6896 } 6897 else 6898 continue; 6899 6900 /* Did we find this language already? */ 6901 for (j = 0; j < ga.ga_len; j += 2) 6902 if (STRNCMP(lang, ((char_u *)ga.ga_data) + j, 2) == 0) 6903 break; 6904 if (j == ga.ga_len) 6905 { 6906 /* New language, add it. */ 6907 if (ga_grow(&ga, 2) == FAIL) 6908 break; 6909 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[0]; 6910 ((char_u *)ga.ga_data)[ga.ga_len++] = lang[1]; 6911 } 6912 } 6913 } 6914 6915 /* 6916 * Loop over the found languages to generate a tags file for each one. 6917 */ 6918 for (j = 0; j < ga.ga_len; j += 2) 6919 { 6920 STRCPY(fname, "tags-xx"); 6921 fname[5] = ((char_u *)ga.ga_data)[j]; 6922 fname[6] = ((char_u *)ga.ga_data)[j + 1]; 6923 if (fname[5] == 'e' && fname[6] == 'n') 6924 { 6925 /* English is an exception: use ".txt" and "tags". */ 6926 fname[4] = NUL; 6927 STRCPY(ext, ".txt"); 6928 } 6929 else 6930 { 6931 /* Language "ab" uses ".abx" and "tags-ab". */ 6932 STRCPY(ext, ".xxx"); 6933 ext[1] = fname[5]; 6934 ext[2] = fname[6]; 6935 } 6936 helptags_one(dirname, ext, fname, add_help_tags); 6937 } 6938 6939 ga_clear(&ga); 6940 FreeWild(filecount, files); 6941 6942 #else 6943 /* No language support, just use "*.txt" and "tags". */ 6944 helptags_one(dirname, (char_u *)".txt", (char_u *)"tags", add_help_tags); 6945 #endif 6946 } 6947 6948 static void 6949 helptags_cb(char_u *fname, void *cookie) 6950 { 6951 do_helptags(fname, *(int *)cookie); 6952 } 6953 6954 /* 6955 * ":helptags" 6956 */ 6957 void 6958 ex_helptags(exarg_T *eap) 6959 { 6960 expand_T xpc; 6961 char_u *dirname; 6962 int add_help_tags = FALSE; 6963 6964 /* Check for ":helptags ++t {dir}". */ 6965 if (STRNCMP(eap->arg, "++t", 3) == 0 && vim_iswhite(eap->arg[3])) 6966 { 6967 add_help_tags = TRUE; 6968 eap->arg = skipwhite(eap->arg + 3); 6969 } 6970 6971 if (STRCMP(eap->arg, "ALL") == 0) 6972 { 6973 do_in_path(p_rtp, (char_u *)"doc", DIP_ALL + DIP_DIR, 6974 helptags_cb, &add_help_tags); 6975 } 6976 else 6977 { 6978 ExpandInit(&xpc); 6979 xpc.xp_context = EXPAND_DIRECTORIES; 6980 dirname = ExpandOne(&xpc, eap->arg, NULL, 6981 WILD_LIST_NOTFOUND|WILD_SILENT, WILD_EXPAND_FREE); 6982 if (dirname == NULL || !mch_isdir(dirname)) 6983 EMSG2(_("E150: Not a directory: %s"), eap->arg); 6984 else 6985 do_helptags(dirname, add_help_tags); 6986 vim_free(dirname); 6987 } 6988 } 6989 6990 #if defined(FEAT_SIGNS) || defined(PROTO) 6991 6992 /* 6993 * Struct to hold the sign properties. 6994 */ 6995 typedef struct sign sign_T; 6996 6997 struct sign 6998 { 6999 sign_T *sn_next; /* next sign in list */ 7000 int sn_typenr; /* type number of sign */ 7001 char_u *sn_name; /* name of sign */ 7002 char_u *sn_icon; /* name of pixmap */ 7003 # ifdef FEAT_SIGN_ICONS 7004 void *sn_image; /* icon image */ 7005 # endif 7006 char_u *sn_text; /* text used instead of pixmap */ 7007 int sn_line_hl; /* highlight ID for line */ 7008 int sn_text_hl; /* highlight ID for text */ 7009 }; 7010 7011 static sign_T *first_sign = NULL; 7012 static int next_sign_typenr = 1; 7013 7014 static int sign_cmd_idx(char_u *begin_cmd, char_u *end_cmd); 7015 static void sign_list_defined(sign_T *sp); 7016 static void sign_undefine(sign_T *sp, sign_T *sp_prev); 7017 7018 static char *cmds[] = { 7019 "define", 7020 # define SIGNCMD_DEFINE 0 7021 "undefine", 7022 # define SIGNCMD_UNDEFINE 1 7023 "list", 7024 # define SIGNCMD_LIST 2 7025 "place", 7026 # define SIGNCMD_PLACE 3 7027 "unplace", 7028 # define SIGNCMD_UNPLACE 4 7029 "jump", 7030 # define SIGNCMD_JUMP 5 7031 NULL 7032 # define SIGNCMD_LAST 6 7033 }; 7034 7035 /* 7036 * Find index of a ":sign" subcmd from its name. 7037 * "*end_cmd" must be writable. 7038 */ 7039 static int 7040 sign_cmd_idx( 7041 char_u *begin_cmd, /* begin of sign subcmd */ 7042 char_u *end_cmd) /* just after sign subcmd */ 7043 { 7044 int idx; 7045 char save = *end_cmd; 7046 7047 *end_cmd = NUL; 7048 for (idx = 0; ; ++idx) 7049 if (cmds[idx] == NULL || STRCMP(begin_cmd, cmds[idx]) == 0) 7050 break; 7051 *end_cmd = save; 7052 return idx; 7053 } 7054 7055 /* 7056 * ":sign" command 7057 */ 7058 void 7059 ex_sign(exarg_T *eap) 7060 { 7061 char_u *arg = eap->arg; 7062 char_u *p; 7063 int idx; 7064 sign_T *sp; 7065 sign_T *sp_prev; 7066 buf_T *buf; 7067 7068 /* Parse the subcommand. */ 7069 p = skiptowhite(arg); 7070 idx = sign_cmd_idx(arg, p); 7071 if (idx == SIGNCMD_LAST) 7072 { 7073 EMSG2(_("E160: Unknown sign command: %s"), arg); 7074 return; 7075 } 7076 arg = skipwhite(p); 7077 7078 if (idx <= SIGNCMD_LIST) 7079 { 7080 /* 7081 * Define, undefine or list signs. 7082 */ 7083 if (idx == SIGNCMD_LIST && *arg == NUL) 7084 { 7085 /* ":sign list": list all defined signs */ 7086 for (sp = first_sign; sp != NULL && !got_int; sp = sp->sn_next) 7087 sign_list_defined(sp); 7088 } 7089 else if (*arg == NUL) 7090 EMSG(_("E156: Missing sign name")); 7091 else 7092 { 7093 /* Isolate the sign name. If it's a number skip leading zeroes, 7094 * so that "099" and "99" are the same sign. But keep "0". */ 7095 p = skiptowhite(arg); 7096 if (*p != NUL) 7097 *p++ = NUL; 7098 while (arg[0] == '0' && arg[1] != NUL) 7099 ++arg; 7100 7101 sp_prev = NULL; 7102 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7103 { 7104 if (STRCMP(sp->sn_name, arg) == 0) 7105 break; 7106 sp_prev = sp; 7107 } 7108 if (idx == SIGNCMD_DEFINE) 7109 { 7110 /* ":sign define {name} ...": define a sign */ 7111 if (sp == NULL) 7112 { 7113 sign_T *lp; 7114 int start = next_sign_typenr; 7115 7116 /* Allocate a new sign. */ 7117 sp = (sign_T *)alloc_clear((unsigned)sizeof(sign_T)); 7118 if (sp == NULL) 7119 return; 7120 7121 /* Check that next_sign_typenr is not already being used. 7122 * This only happens after wrapping around. Hopefully 7123 * another one got deleted and we can use its number. */ 7124 for (lp = first_sign; lp != NULL; ) 7125 { 7126 if (lp->sn_typenr == next_sign_typenr) 7127 { 7128 ++next_sign_typenr; 7129 if (next_sign_typenr == MAX_TYPENR) 7130 next_sign_typenr = 1; 7131 if (next_sign_typenr == start) 7132 { 7133 vim_free(sp); 7134 EMSG(_("E612: Too many signs defined")); 7135 return; 7136 } 7137 lp = first_sign; /* start all over */ 7138 continue; 7139 } 7140 lp = lp->sn_next; 7141 } 7142 7143 sp->sn_typenr = next_sign_typenr; 7144 if (++next_sign_typenr == MAX_TYPENR) 7145 next_sign_typenr = 1; /* wrap around */ 7146 7147 sp->sn_name = vim_strsave(arg); 7148 if (sp->sn_name == NULL) /* out of memory */ 7149 { 7150 vim_free(sp); 7151 return; 7152 } 7153 7154 /* add the new sign to the list of signs */ 7155 if (sp_prev == NULL) 7156 first_sign = sp; 7157 else 7158 sp_prev->sn_next = sp; 7159 } 7160 7161 /* set values for a defined sign. */ 7162 for (;;) 7163 { 7164 arg = skipwhite(p); 7165 if (*arg == NUL) 7166 break; 7167 p = skiptowhite_esc(arg); 7168 if (STRNCMP(arg, "icon=", 5) == 0) 7169 { 7170 arg += 5; 7171 vim_free(sp->sn_icon); 7172 sp->sn_icon = vim_strnsave(arg, (int)(p - arg)); 7173 backslash_halve(sp->sn_icon); 7174 # ifdef FEAT_SIGN_ICONS 7175 if (gui.in_use) 7176 { 7177 out_flush(); 7178 if (sp->sn_image != NULL) 7179 gui_mch_destroy_sign(sp->sn_image); 7180 sp->sn_image = gui_mch_register_sign(sp->sn_icon); 7181 } 7182 # endif 7183 } 7184 else if (STRNCMP(arg, "text=", 5) == 0) 7185 { 7186 char_u *s; 7187 int cells; 7188 int len; 7189 7190 arg += 5; 7191 # ifdef FEAT_MBYTE 7192 /* Count cells and check for non-printable chars */ 7193 if (has_mbyte) 7194 { 7195 cells = 0; 7196 for (s = arg; s < p; s += (*mb_ptr2len)(s)) 7197 { 7198 if (!vim_isprintc((*mb_ptr2char)(s))) 7199 break; 7200 cells += (*mb_ptr2cells)(s); 7201 } 7202 } 7203 else 7204 # endif 7205 { 7206 for (s = arg; s < p; ++s) 7207 if (!vim_isprintc(*s)) 7208 break; 7209 cells = (int)(s - arg); 7210 } 7211 /* Currently must be one or two display cells */ 7212 if (s != p || cells < 1 || cells > 2) 7213 { 7214 *p = NUL; 7215 EMSG2(_("E239: Invalid sign text: %s"), arg); 7216 return; 7217 } 7218 7219 vim_free(sp->sn_text); 7220 /* Allocate one byte more if we need to pad up 7221 * with a space. */ 7222 len = (int)(p - arg + ((cells == 1) ? 1 : 0)); 7223 sp->sn_text = vim_strnsave(arg, len); 7224 7225 if (sp->sn_text != NULL && cells == 1) 7226 STRCPY(sp->sn_text + len - 1, " "); 7227 } 7228 else if (STRNCMP(arg, "linehl=", 7) == 0) 7229 { 7230 arg += 7; 7231 sp->sn_line_hl = syn_check_group(arg, (int)(p - arg)); 7232 } 7233 else if (STRNCMP(arg, "texthl=", 7) == 0) 7234 { 7235 arg += 7; 7236 sp->sn_text_hl = syn_check_group(arg, (int)(p - arg)); 7237 } 7238 else 7239 { 7240 EMSG2(_(e_invarg2), arg); 7241 return; 7242 } 7243 } 7244 } 7245 else if (sp == NULL) 7246 EMSG2(_("E155: Unknown sign: %s"), arg); 7247 else if (idx == SIGNCMD_LIST) 7248 /* ":sign list {name}" */ 7249 sign_list_defined(sp); 7250 else 7251 /* ":sign undefine {name}" */ 7252 sign_undefine(sp, sp_prev); 7253 } 7254 } 7255 else 7256 { 7257 int id = -1; 7258 linenr_T lnum = -1; 7259 char_u *sign_name = NULL; 7260 char_u *arg1; 7261 7262 if (*arg == NUL) 7263 { 7264 if (idx == SIGNCMD_PLACE) 7265 { 7266 /* ":sign place": list placed signs in all buffers */ 7267 sign_list_placed(NULL); 7268 } 7269 else if (idx == SIGNCMD_UNPLACE) 7270 { 7271 /* ":sign unplace": remove placed sign at cursor */ 7272 id = buf_findsign_id(curwin->w_buffer, curwin->w_cursor.lnum); 7273 if (id > 0) 7274 { 7275 buf_delsign(curwin->w_buffer, id); 7276 update_debug_sign(curwin->w_buffer, curwin->w_cursor.lnum); 7277 } 7278 else 7279 EMSG(_("E159: Missing sign number")); 7280 } 7281 else 7282 EMSG(_(e_argreq)); 7283 return; 7284 } 7285 7286 if (idx == SIGNCMD_UNPLACE && arg[0] == '*' && arg[1] == NUL) 7287 { 7288 /* ":sign unplace *": remove all placed signs */ 7289 buf_delete_all_signs(); 7290 return; 7291 } 7292 7293 /* first arg could be placed sign id */ 7294 arg1 = arg; 7295 if (VIM_ISDIGIT(*arg)) 7296 { 7297 id = getdigits(&arg); 7298 if (!vim_iswhite(*arg) && *arg != NUL) 7299 { 7300 id = -1; 7301 arg = arg1; 7302 } 7303 else 7304 { 7305 arg = skipwhite(arg); 7306 if (idx == SIGNCMD_UNPLACE && *arg == NUL) 7307 { 7308 /* ":sign unplace {id}": remove placed sign by number */ 7309 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 7310 if ((lnum = buf_delsign(buf, id)) != 0) 7311 update_debug_sign(buf, lnum); 7312 return; 7313 } 7314 } 7315 } 7316 7317 /* 7318 * Check for line={lnum} name={name} and file={fname} or buffer={nr}. 7319 * Leave "arg" pointing to {fname}. 7320 */ 7321 for (;;) 7322 { 7323 if (STRNCMP(arg, "line=", 5) == 0) 7324 { 7325 arg += 5; 7326 lnum = atoi((char *)arg); 7327 arg = skiptowhite(arg); 7328 } 7329 else if (STRNCMP(arg, "*", 1) == 0 && idx == SIGNCMD_UNPLACE) 7330 { 7331 if (id != -1) 7332 { 7333 EMSG(_(e_invarg)); 7334 return; 7335 } 7336 id = -2; 7337 arg = skiptowhite(arg + 1); 7338 } 7339 else if (STRNCMP(arg, "name=", 5) == 0) 7340 { 7341 arg += 5; 7342 sign_name = arg; 7343 arg = skiptowhite(arg); 7344 if (*arg != NUL) 7345 *arg++ = NUL; 7346 while (sign_name[0] == '0' && sign_name[1] != NUL) 7347 ++sign_name; 7348 } 7349 else if (STRNCMP(arg, "file=", 5) == 0) 7350 { 7351 arg += 5; 7352 buf = buflist_findname(arg); 7353 break; 7354 } 7355 else if (STRNCMP(arg, "buffer=", 7) == 0) 7356 { 7357 arg += 7; 7358 buf = buflist_findnr((int)getdigits(&arg)); 7359 if (*skipwhite(arg) != NUL) 7360 EMSG(_(e_trailing)); 7361 break; 7362 } 7363 else 7364 { 7365 EMSG(_(e_invarg)); 7366 return; 7367 } 7368 arg = skipwhite(arg); 7369 } 7370 7371 if (buf == NULL) 7372 { 7373 EMSG2(_("E158: Invalid buffer name: %s"), arg); 7374 } 7375 else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2)) 7376 { 7377 if (lnum >= 0 || sign_name != NULL) 7378 EMSG(_(e_invarg)); 7379 else 7380 /* ":sign place file={fname}": list placed signs in one file */ 7381 sign_list_placed(buf); 7382 } 7383 else if (idx == SIGNCMD_JUMP) 7384 { 7385 /* ":sign jump {id} file={fname}" */ 7386 if (lnum >= 0 || sign_name != NULL) 7387 EMSG(_(e_invarg)); 7388 else if ((lnum = buf_findsign(buf, id)) > 0) 7389 { /* goto a sign ... */ 7390 if (buf_jump_open_win(buf) != NULL) 7391 { /* ... in a current window */ 7392 curwin->w_cursor.lnum = lnum; 7393 check_cursor_lnum(); 7394 beginline(BL_WHITE); 7395 } 7396 else 7397 { /* ... not currently in a window */ 7398 char_u *cmd; 7399 7400 cmd = alloc((unsigned)STRLEN(buf->b_fname) + 25); 7401 if (cmd == NULL) 7402 return; 7403 sprintf((char *)cmd, "e +%ld %s", (long)lnum, buf->b_fname); 7404 do_cmdline_cmd(cmd); 7405 vim_free(cmd); 7406 } 7407 # ifdef FEAT_FOLDING 7408 foldOpenCursor(); 7409 # endif 7410 } 7411 else 7412 EMSGN(_("E157: Invalid sign ID: %ld"), id); 7413 } 7414 else if (idx == SIGNCMD_UNPLACE) 7415 { 7416 if (lnum >= 0 || sign_name != NULL) 7417 EMSG(_(e_invarg)); 7418 else if (id == -2) 7419 { 7420 /* ":sign unplace * file={fname}" */ 7421 redraw_buf_later(buf, NOT_VALID); 7422 buf_delete_signs(buf); 7423 } 7424 else 7425 { 7426 /* ":sign unplace {id} file={fname}" */ 7427 lnum = buf_delsign(buf, id); 7428 update_debug_sign(buf, lnum); 7429 } 7430 } 7431 /* idx == SIGNCMD_PLACE */ 7432 else if (sign_name != NULL) 7433 { 7434 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7435 if (STRCMP(sp->sn_name, sign_name) == 0) 7436 break; 7437 if (sp == NULL) 7438 { 7439 EMSG2(_("E155: Unknown sign: %s"), sign_name); 7440 return; 7441 } 7442 if (lnum > 0) 7443 /* ":sign place {id} line={lnum} name={name} file={fname}": 7444 * place a sign */ 7445 buf_addsign(buf, id, lnum, sp->sn_typenr); 7446 else 7447 /* ":sign place {id} file={fname}": change sign type */ 7448 lnum = buf_change_sign_type(buf, id, sp->sn_typenr); 7449 if (lnum > 0) 7450 update_debug_sign(buf, lnum); 7451 else 7452 EMSG2(_("E885: Not possible to change sign %s"), sign_name); 7453 } 7454 else 7455 EMSG(_(e_invarg)); 7456 } 7457 } 7458 7459 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) 7460 /* 7461 * Allocate the icons. Called when the GUI has started. Allows defining 7462 * signs before it starts. 7463 */ 7464 void 7465 sign_gui_started(void) 7466 { 7467 sign_T *sp; 7468 7469 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7470 if (sp->sn_icon != NULL) 7471 sp->sn_image = gui_mch_register_sign(sp->sn_icon); 7472 } 7473 # endif 7474 7475 /* 7476 * List one sign. 7477 */ 7478 static void 7479 sign_list_defined(sign_T *sp) 7480 { 7481 char_u *p; 7482 7483 smsg((char_u *)"sign %s", sp->sn_name); 7484 if (sp->sn_icon != NULL) 7485 { 7486 MSG_PUTS(" icon="); 7487 msg_outtrans(sp->sn_icon); 7488 # ifdef FEAT_SIGN_ICONS 7489 if (sp->sn_image == NULL) 7490 MSG_PUTS(_(" (NOT FOUND)")); 7491 # else 7492 MSG_PUTS(_(" (not supported)")); 7493 # endif 7494 } 7495 if (sp->sn_text != NULL) 7496 { 7497 MSG_PUTS(" text="); 7498 msg_outtrans(sp->sn_text); 7499 } 7500 if (sp->sn_line_hl > 0) 7501 { 7502 MSG_PUTS(" linehl="); 7503 p = get_highlight_name(NULL, sp->sn_line_hl - 1); 7504 if (p == NULL) 7505 MSG_PUTS("NONE"); 7506 else 7507 msg_puts(p); 7508 } 7509 if (sp->sn_text_hl > 0) 7510 { 7511 MSG_PUTS(" texthl="); 7512 p = get_highlight_name(NULL, sp->sn_text_hl - 1); 7513 if (p == NULL) 7514 MSG_PUTS("NONE"); 7515 else 7516 msg_puts(p); 7517 } 7518 } 7519 7520 /* 7521 * Undefine a sign and free its memory. 7522 */ 7523 static void 7524 sign_undefine(sign_T *sp, sign_T *sp_prev) 7525 { 7526 vim_free(sp->sn_name); 7527 vim_free(sp->sn_icon); 7528 # ifdef FEAT_SIGN_ICONS 7529 if (sp->sn_image != NULL) 7530 { 7531 out_flush(); 7532 gui_mch_destroy_sign(sp->sn_image); 7533 } 7534 # endif 7535 vim_free(sp->sn_text); 7536 if (sp_prev == NULL) 7537 first_sign = sp->sn_next; 7538 else 7539 sp_prev->sn_next = sp->sn_next; 7540 vim_free(sp); 7541 } 7542 7543 /* 7544 * Get highlighting attribute for sign "typenr". 7545 * If "line" is TRUE: line highl, if FALSE: text highl. 7546 */ 7547 int 7548 sign_get_attr(int typenr, int line) 7549 { 7550 sign_T *sp; 7551 7552 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7553 if (sp->sn_typenr == typenr) 7554 { 7555 if (line) 7556 { 7557 if (sp->sn_line_hl > 0) 7558 return syn_id2attr(sp->sn_line_hl); 7559 } 7560 else 7561 { 7562 if (sp->sn_text_hl > 0) 7563 return syn_id2attr(sp->sn_text_hl); 7564 } 7565 break; 7566 } 7567 return 0; 7568 } 7569 7570 /* 7571 * Get text mark for sign "typenr". 7572 * Returns NULL if there isn't one. 7573 */ 7574 char_u * 7575 sign_get_text(int typenr) 7576 { 7577 sign_T *sp; 7578 7579 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7580 if (sp->sn_typenr == typenr) 7581 return sp->sn_text; 7582 return NULL; 7583 } 7584 7585 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) 7586 void * 7587 sign_get_image( 7588 int typenr) /* the attribute which may have a sign */ 7589 { 7590 sign_T *sp; 7591 7592 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7593 if (sp->sn_typenr == typenr) 7594 return sp->sn_image; 7595 return NULL; 7596 } 7597 # endif 7598 7599 /* 7600 * Get the name of a sign by its typenr. 7601 */ 7602 char_u * 7603 sign_typenr2name(int typenr) 7604 { 7605 sign_T *sp; 7606 7607 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7608 if (sp->sn_typenr == typenr) 7609 return sp->sn_name; 7610 return (char_u *)_("[Deleted]"); 7611 } 7612 7613 # if defined(EXITFREE) || defined(PROTO) 7614 /* 7615 * Undefine/free all signs. 7616 */ 7617 void 7618 free_signs(void) 7619 { 7620 while (first_sign != NULL) 7621 sign_undefine(first_sign, NULL); 7622 } 7623 # endif 7624 7625 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) 7626 static enum 7627 { 7628 EXP_SUBCMD, /* expand :sign sub-commands */ 7629 EXP_DEFINE, /* expand :sign define {name} args */ 7630 EXP_PLACE, /* expand :sign place {id} args */ 7631 EXP_UNPLACE, /* expand :sign unplace" */ 7632 EXP_SIGN_NAMES /* expand with name of placed signs */ 7633 } expand_what; 7634 7635 /* 7636 * Function given to ExpandGeneric() to obtain the sign command 7637 * expansion. 7638 */ 7639 char_u * 7640 get_sign_name(expand_T *xp UNUSED, int idx) 7641 { 7642 sign_T *sp; 7643 int current_idx; 7644 7645 switch (expand_what) 7646 { 7647 case EXP_SUBCMD: 7648 return (char_u *)cmds[idx]; 7649 case EXP_DEFINE: 7650 { 7651 char *define_arg[] = 7652 { 7653 "icon=", "linehl=", "text=", "texthl=", NULL 7654 }; 7655 return (char_u *)define_arg[idx]; 7656 } 7657 case EXP_PLACE: 7658 { 7659 char *place_arg[] = 7660 { 7661 "line=", "name=", "file=", "buffer=", NULL 7662 }; 7663 return (char_u *)place_arg[idx]; 7664 } 7665 case EXP_UNPLACE: 7666 { 7667 char *unplace_arg[] = { "file=", "buffer=", NULL }; 7668 return (char_u *)unplace_arg[idx]; 7669 } 7670 case EXP_SIGN_NAMES: 7671 /* Complete with name of signs already defined */ 7672 current_idx = 0; 7673 for (sp = first_sign; sp != NULL; sp = sp->sn_next) 7674 if (current_idx++ == idx) 7675 return sp->sn_name; 7676 return NULL; 7677 default: 7678 return NULL; 7679 } 7680 } 7681 7682 /* 7683 * Handle command line completion for :sign command. 7684 */ 7685 void 7686 set_context_in_sign_cmd(expand_T *xp, char_u *arg) 7687 { 7688 char_u *p; 7689 char_u *end_subcmd; 7690 char_u *last; 7691 int cmd_idx; 7692 char_u *begin_subcmd_args; 7693 7694 /* Default: expand subcommands. */ 7695 xp->xp_context = EXPAND_SIGN; 7696 expand_what = EXP_SUBCMD; 7697 xp->xp_pattern = arg; 7698 7699 end_subcmd = skiptowhite(arg); 7700 if (*end_subcmd == NUL) 7701 /* expand subcmd name 7702 * :sign {subcmd}<CTRL-D>*/ 7703 return; 7704 7705 cmd_idx = sign_cmd_idx(arg, end_subcmd); 7706 7707 /* :sign {subcmd} {subcmd_args} 7708 * | 7709 * begin_subcmd_args */ 7710 begin_subcmd_args = skipwhite(end_subcmd); 7711 p = skiptowhite(begin_subcmd_args); 7712 if (*p == NUL) 7713 { 7714 /* 7715 * Expand first argument of subcmd when possible. 7716 * For ":jump {id}" and ":unplace {id}", we could 7717 * possibly expand the ids of all signs already placed. 7718 */ 7719 xp->xp_pattern = begin_subcmd_args; 7720 switch (cmd_idx) 7721 { 7722 case SIGNCMD_LIST: 7723 case SIGNCMD_UNDEFINE: 7724 /* :sign list <CTRL-D> 7725 * :sign undefine <CTRL-D> */ 7726 expand_what = EXP_SIGN_NAMES; 7727 break; 7728 default: 7729 xp->xp_context = EXPAND_NOTHING; 7730 } 7731 return; 7732 } 7733 7734 /* expand last argument of subcmd */ 7735 7736 /* :sign define {name} {args}... 7737 * | 7738 * p */ 7739 7740 /* Loop until reaching last argument. */ 7741 do 7742 { 7743 p = skipwhite(p); 7744 last = p; 7745 p = skiptowhite(p); 7746 } while (*p != NUL); 7747 7748 p = vim_strchr(last, '='); 7749 7750 /* :sign define {name} {args}... {last}= 7751 * | | 7752 * last p */ 7753 if (p == NUL) 7754 { 7755 /* Expand last argument name (before equal sign). */ 7756 xp->xp_pattern = last; 7757 switch (cmd_idx) 7758 { 7759 case SIGNCMD_DEFINE: 7760 expand_what = EXP_DEFINE; 7761 break; 7762 case SIGNCMD_PLACE: 7763 expand_what = EXP_PLACE; 7764 break; 7765 case SIGNCMD_JUMP: 7766 case SIGNCMD_UNPLACE: 7767 expand_what = EXP_UNPLACE; 7768 break; 7769 default: 7770 xp->xp_context = EXPAND_NOTHING; 7771 } 7772 } 7773 else 7774 { 7775 /* Expand last argument value (after equal sign). */ 7776 xp->xp_pattern = p + 1; 7777 switch (cmd_idx) 7778 { 7779 case SIGNCMD_DEFINE: 7780 if (STRNCMP(last, "texthl", p - last) == 0 || 7781 STRNCMP(last, "linehl", p - last) == 0) 7782 xp->xp_context = EXPAND_HIGHLIGHT; 7783 else if (STRNCMP(last, "icon", p - last) == 0) 7784 xp->xp_context = EXPAND_FILES; 7785 else 7786 xp->xp_context = EXPAND_NOTHING; 7787 break; 7788 case SIGNCMD_PLACE: 7789 if (STRNCMP(last, "name", p - last) == 0) 7790 expand_what = EXP_SIGN_NAMES; 7791 else 7792 xp->xp_context = EXPAND_NOTHING; 7793 break; 7794 default: 7795 xp->xp_context = EXPAND_NOTHING; 7796 } 7797 } 7798 } 7799 # endif 7800 #endif 7801 7802 /* 7803 * Make the user happy. 7804 */ 7805 void 7806 ex_smile(exarg_T *eap UNUSED) 7807 { 7808 static char *code = "\34 \4o\14$\4ox\30 \2o\30$\1ox\25 \2o\36$\1o\11 \1o\1$\3 \2$\1 \1o\1$x\5 \1o\1 \1$\1 \2o\10 \1o\44$\1o\7 \2$\1 \2$\1 \2$\1o\1$x\2 \2o\1 \1$\1 \1$\1 \1\"\1$\6 \1o\11$\4 \15$\4 \11$\1o\7 \3$\1o\2$\1o\1$x\2 \1\"\6$\1o\1$\5 \1o\11$\6 \13$\6 \12$\1o\4 \10$x\4 \7$\4 \13$\6 \13$\6 \27$x\4 \27$\4 \15$\4 \16$\2 \3\"\3$x\5 \1\"\3$\4\"\61$\5 \1\"\3$x\6 \3$\3 \1o\62$\5 \1\"\3$\1ox\5 \1o\2$\1\"\3 \63$\7 \3$\1ox\5 \3$\4 \55$\1\"\1 \1\"\6$\5o\4$\1ox\4 \1o\3$\4o\5$\2 \45$\3 \1o\21$x\4 \10$\1\"\4$\3 \42$\5 \4$\10\"x\3 \4\"\7 \4$\4 \1\"\34$\1\"\6 \1o\3$x\16 \1\"\3$\1o\5 \3\"\22$\1\"\2$\1\"\11 \3$x\20 \3$\1o\12 \1\"\2$\2\"\6$\4\"\13 \1o\3$x\21 \4$\1o\40 \1o\3$\1\"x\22 \1\"\4$\1o\6 \1o\6$\1o\1\"\4$\1o\10 \1o\4$x\24 \1\"\5$\2o\5 \2\"\4$\1o\5$\1o\3 \1o\4$\2\"x\27 \2\"\5$\4o\2 \1\"\3$\1o\11$\3\"x\32 \2\"\7$\2o\1 \12$x\42 \4\"\13$x\46 \14$x\47 \12$\1\"x\50 \1\"\3$\4\"x"; 7809 char *p; 7810 int n; 7811 7812 msg_start(); 7813 msg_putchar('\n'); 7814 for (p = code; *p != NUL; ++p) 7815 if (*p == 'x') 7816 msg_putchar('\n'); 7817 else 7818 for (n = *p++; n > 0; --n) 7819 if (*p == 'o' || *p == '$') 7820 msg_putchar_attr(*p, hl_attr(HLF_L)); 7821 else 7822 msg_putchar(*p); 7823 msg_clr_eos(); 7824 } 7825 7826 #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) 7827 /* 7828 * ":drop" 7829 * Opens the first argument in a window. When there are two or more arguments 7830 * the argument list is redefined. 7831 */ 7832 void 7833 ex_drop(exarg_T *eap) 7834 { 7835 int split = FALSE; 7836 win_T *wp; 7837 buf_T *buf; 7838 # ifdef FEAT_WINDOWS 7839 tabpage_T *tp; 7840 # endif 7841 7842 /* 7843 * Check if the first argument is already being edited in a window. If 7844 * so, jump to that window. 7845 * We would actually need to check all arguments, but that's complicated 7846 * and mostly only one file is dropped. 7847 * This also ignores wildcards, since it is very unlikely the user is 7848 * editing a file name with a wildcard character. 7849 */ 7850 set_arglist(eap->arg); 7851 7852 /* 7853 * Expanding wildcards may result in an empty argument list. E.g. when 7854 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we 7855 * already did an error message for this. 7856 */ 7857 if (ARGCOUNT == 0) 7858 return; 7859 7860 # ifdef FEAT_WINDOWS 7861 if (cmdmod.tab) 7862 { 7863 /* ":tab drop file ...": open a tab for each argument that isn't 7864 * edited in a window yet. It's like ":tab all" but without closing 7865 * windows or tabs. */ 7866 ex_all(eap); 7867 } 7868 else 7869 # endif 7870 { 7871 /* ":drop file ...": Edit the first argument. Jump to an existing 7872 * window if possible, edit in current window if the current buffer 7873 * can be abandoned, otherwise open a new window. */ 7874 buf = buflist_findnr(ARGLIST[0].ae_fnum); 7875 7876 FOR_ALL_TAB_WINDOWS(tp, wp) 7877 { 7878 if (wp->w_buffer == buf) 7879 { 7880 # ifdef FEAT_WINDOWS 7881 goto_tabpage_win(tp, wp); 7882 # endif 7883 curwin->w_arg_idx = 0; 7884 return; 7885 } 7886 } 7887 7888 /* 7889 * Check whether the current buffer is changed. If so, we will need 7890 * to split the current window or data could be lost. 7891 * Skip the check if the 'hidden' option is set, as in this case the 7892 * buffer won't be lost. 7893 */ 7894 if (!P_HID(curbuf)) 7895 { 7896 # ifdef FEAT_WINDOWS 7897 ++emsg_off; 7898 # endif 7899 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD); 7900 # ifdef FEAT_WINDOWS 7901 --emsg_off; 7902 # else 7903 if (split) 7904 return; 7905 # endif 7906 } 7907 7908 /* Fake a ":sfirst" or ":first" command edit the first argument. */ 7909 if (split) 7910 { 7911 eap->cmdidx = CMD_sfirst; 7912 eap->cmd[0] = 's'; 7913 } 7914 else 7915 eap->cmdidx = CMD_first; 7916 ex_rewind(eap); 7917 } 7918 } 7919 #endif 7920