1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * 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 static int not_writing(void); 24 static int check_readonly(int *forceit, buf_T *buf); 25 static void delbuf_msg(char_u *name); 26 27 /* 28 * ":ascii" and "ga". 29 */ 30 void 31 do_ascii(exarg_T *eap UNUSED) 32 { 33 int c; 34 int cval; 35 char buf1[20]; 36 char buf2[20]; 37 char_u buf3[7]; 38 #ifdef FEAT_DIGRAPHS 39 char_u *dig; 40 #endif 41 int cc[MAX_MCO]; 42 int ci = 0; 43 int len; 44 45 if (enc_utf8) 46 c = utfc_ptr2char(ml_get_cursor(), cc); 47 else 48 c = gchar_cursor(); 49 if (c == NUL) 50 { 51 msg("NUL"); 52 return; 53 } 54 55 IObuff[0] = NUL; 56 if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) 57 { 58 if (c == NL) // NUL is stored as NL 59 c = NUL; 60 if (c == CAR && get_fileformat(curbuf) == EOL_MAC) 61 cval = NL; // NL is stored as CR 62 else 63 cval = c; 64 if (vim_isprintc_strict(c) && (c < ' ' 65 #ifndef EBCDIC 66 || c > '~' 67 #endif 68 )) 69 { 70 transchar_nonprint(curbuf, buf3, c); 71 vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); 72 } 73 else 74 buf1[0] = NUL; 75 #ifndef EBCDIC 76 if (c >= 0x80) 77 vim_snprintf(buf2, sizeof(buf2), " <M-%s>", 78 (char *)transchar(c & 0x7f)); 79 else 80 #endif 81 buf2[0] = NUL; 82 #ifdef FEAT_DIGRAPHS 83 dig = get_digraph_for_char(cval); 84 if (dig != NULL) 85 vim_snprintf((char *)IObuff, IOSIZE, 86 _("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"), 87 transchar(c), buf1, buf2, cval, cval, cval, dig); 88 else 89 #endif 90 vim_snprintf((char *)IObuff, IOSIZE, 91 _("<%s>%s%s %d, Hex %02x, Octal %03o"), 92 transchar(c), buf1, buf2, cval, cval, cval); 93 if (enc_utf8) 94 c = cc[ci++]; 95 else 96 c = 0; 97 } 98 99 // Repeat for combining characters. 100 while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80))) 101 { 102 len = (int)STRLEN(IObuff); 103 // This assumes every multi-byte char is printable... 104 if (len > 0) 105 IObuff[len++] = ' '; 106 IObuff[len++] = '<'; 107 if (enc_utf8 && utf_iscomposing(c) 108 # ifdef USE_GUI 109 && !gui.in_use 110 # endif 111 ) 112 IObuff[len++] = ' '; // draw composing char on top of a space 113 len += (*mb_char2bytes)(c, IObuff + len); 114 #ifdef FEAT_DIGRAPHS 115 dig = get_digraph_for_char(c); 116 if (dig != NULL) 117 vim_snprintf((char *)IObuff + len, IOSIZE - len, 118 c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s") 119 : _("> %d, Hex %08x, Oct %o, Digr %s"), 120 c, c, c, dig); 121 else 122 #endif 123 vim_snprintf((char *)IObuff + len, IOSIZE - len, 124 c < 0x10000 ? _("> %d, Hex %04x, Octal %o") 125 : _("> %d, Hex %08x, Octal %o"), 126 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 135 msg((char *)IObuff); 136 } 137 138 /* 139 * ":left", ":center" and ":right": align text. 140 */ 141 void 142 ex_align(exarg_T *eap) 143 { 144 pos_T save_curpos; 145 int len; 146 int indent = 0; 147 int new_indent; 148 int has_tab; 149 int width; 150 151 #ifdef FEAT_RIGHTLEFT 152 if (curwin->w_p_rl) 153 { 154 // switch left and right aligning 155 if (eap->cmdidx == CMD_right) 156 eap->cmdidx = CMD_left; 157 else if (eap->cmdidx == CMD_left) 158 eap->cmdidx = CMD_right; 159 } 160 #endif 161 162 width = atoi((char *)eap->arg); 163 save_curpos = curwin->w_cursor; 164 if (eap->cmdidx == CMD_left) // width is used for new indent 165 { 166 if (width >= 0) 167 indent = width; 168 } 169 else 170 { 171 /* 172 * if 'textwidth' set, use it 173 * else if 'wrapmargin' set, use it 174 * if invalid value, use 80 175 */ 176 if (width <= 0) 177 width = curbuf->b_p_tw; 178 if (width == 0 && curbuf->b_p_wm > 0) 179 width = curwin->w_width - curbuf->b_p_wm; 180 if (width <= 0) 181 width = 80; 182 } 183 184 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) 185 return; 186 187 for (curwin->w_cursor.lnum = eap->line1; 188 curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum) 189 { 190 if (eap->cmdidx == CMD_left) // left align 191 new_indent = indent; 192 else 193 { 194 has_tab = FALSE; // avoid uninit warnings 195 len = linelen(eap->cmdidx == CMD_right ? &has_tab 196 : NULL) - get_indent(); 197 198 if (len <= 0) // skip blank lines 199 continue; 200 201 if (eap->cmdidx == CMD_center) 202 new_indent = (width - len) / 2; 203 else 204 { 205 new_indent = width - len; // right align 206 207 /* 208 * Make sure that embedded TABs don't make the text go too far 209 * to the right. 210 */ 211 if (has_tab) 212 while (new_indent > 0) 213 { 214 (void)set_indent(new_indent, 0); 215 if (linelen(NULL) <= width) 216 { 217 /* 218 * Now try to move the line as much as possible to 219 * the right. Stop when it moves too far. 220 */ 221 do 222 (void)set_indent(++new_indent, 0); 223 while (linelen(NULL) <= width); 224 --new_indent; 225 break; 226 } 227 --new_indent; 228 } 229 } 230 } 231 if (new_indent < 0) 232 new_indent = 0; 233 (void)set_indent(new_indent, 0); // set indent 234 } 235 changed_lines(eap->line1, 0, eap->line2 + 1, 0L); 236 curwin->w_cursor = save_curpos; 237 beginline(BL_WHITE | BL_FIX); 238 } 239 240 /* 241 * Get the length of the current line, excluding trailing white space. 242 */ 243 static int 244 linelen(int *has_tab) 245 { 246 char_u *line; 247 char_u *first; 248 char_u *last; 249 int save; 250 int len; 251 252 // Get the line. If it's empty bail out early (could be the empty string 253 // for an unloaded buffer). 254 line = ml_get_curline(); 255 if (*line == NUL) 256 return 0; 257 258 // find the first non-blank character 259 first = skipwhite(line); 260 261 // find the character after the last non-blank character 262 for (last = first + STRLEN(first); 263 last > first && VIM_ISWHITE(last[-1]); --last) 264 ; 265 save = *last; 266 *last = NUL; 267 len = linetabsize(line); // get line length 268 if (has_tab != NULL) // check for embedded TAB 269 *has_tab = (vim_strchr(first, TAB) != NULL); 270 *last = save; 271 272 return len; 273 } 274 275 // Buffer for two lines used during sorting. They are allocated to 276 // contain the longest line being sorted. 277 static char_u *sortbuf1; 278 static char_u *sortbuf2; 279 280 static int sort_lc; // sort using locale 281 static int sort_ic; // ignore case 282 static int sort_nr; // sort on number 283 static int sort_rx; // sort on regex instead of skipping it 284 #ifdef FEAT_FLOAT 285 static int sort_flt; // sort on floating number 286 #endif 287 288 static int sort_abort; // flag to indicate if sorting has been interrupted 289 290 // Struct to store info to be sorted. 291 typedef struct 292 { 293 linenr_T lnum; // line number 294 union { 295 struct 296 { 297 varnumber_T start_col_nr; // starting column number 298 varnumber_T end_col_nr; // ending column number 299 } line; 300 struct 301 { 302 varnumber_T value; // value if sorting by integer 303 int is_number; // TRUE when line contains a number 304 } num; 305 #ifdef FEAT_FLOAT 306 float_T value_flt; // value if sorting by float 307 #endif 308 } st_u; 309 } sorti_T; 310 311 static int 312 string_compare(const void *s1, const void *s2) 313 { 314 if (sort_lc) 315 return strcoll((char *)s1, (char *)s2); 316 return sort_ic ? STRICMP(s1, s2) : STRCMP(s1, s2); 317 } 318 319 static int 320 sort_compare(const void *s1, const void *s2) 321 { 322 sorti_T l1 = *(sorti_T *)s1; 323 sorti_T l2 = *(sorti_T *)s2; 324 int result = 0; 325 326 // If the user interrupts, there's no way to stop qsort() immediately, but 327 // if we return 0 every time, qsort will assume it's done sorting and 328 // exit. 329 if (sort_abort) 330 return 0; 331 fast_breakcheck(); 332 if (got_int) 333 sort_abort = TRUE; 334 335 if (sort_nr) 336 { 337 if (l1.st_u.num.is_number != l2.st_u.num.is_number) 338 result = l1.st_u.num.is_number - l2.st_u.num.is_number; 339 else 340 result = l1.st_u.num.value == l2.st_u.num.value ? 0 341 : l1.st_u.num.value > l2.st_u.num.value ? 1 : -1; 342 } 343 #ifdef FEAT_FLOAT 344 else if (sort_flt) 345 result = l1.st_u.value_flt == l2.st_u.value_flt ? 0 346 : l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1; 347 #endif 348 else 349 { 350 // We need to copy one line into "sortbuf1", because there is no 351 // guarantee that the first pointer becomes invalid when obtaining the 352 // second one. 353 STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr, 354 l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1); 355 sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0; 356 STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr, 357 l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1); 358 sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0; 359 360 result = string_compare(sortbuf1, sortbuf2); 361 } 362 363 // If two lines have the same value, preserve the original line order. 364 if (result == 0) 365 return (int)(l1.lnum - l2.lnum); 366 return result; 367 } 368 369 /* 370 * ":sort". 371 */ 372 void 373 ex_sort(exarg_T *eap) 374 { 375 regmatch_T regmatch; 376 int len; 377 linenr_T lnum; 378 long maxlen = 0; 379 sorti_T *nrs; 380 size_t count = (size_t)(eap->line2 - eap->line1 + 1); 381 size_t i; 382 char_u *p; 383 char_u *s; 384 char_u *s2; 385 char_u c; // temporary character storage 386 int unique = FALSE; 387 long deleted; 388 colnr_T start_col; 389 colnr_T end_col; 390 int sort_what = 0; 391 int format_found = 0; 392 int change_occurred = FALSE; // Buffer contents changed. 393 394 // Sorting one line is really quick! 395 if (count <= 1) 396 return; 397 398 if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL) 399 return; 400 sortbuf1 = NULL; 401 sortbuf2 = NULL; 402 regmatch.regprog = NULL; 403 nrs = ALLOC_MULT(sorti_T, count); 404 if (nrs == NULL) 405 goto sortend; 406 407 sort_abort = sort_ic = sort_lc = sort_rx = sort_nr = 0; 408 #ifdef FEAT_FLOAT 409 sort_flt = 0; 410 #endif 411 412 for (p = eap->arg; *p != NUL; ++p) 413 { 414 if (VIM_ISWHITE(*p)) 415 ; 416 else if (*p == 'i') 417 sort_ic = TRUE; 418 else if (*p == 'l') 419 sort_lc = TRUE; 420 else if (*p == 'r') 421 sort_rx = TRUE; 422 else if (*p == 'n') 423 { 424 sort_nr = 1; 425 ++format_found; 426 } 427 #ifdef FEAT_FLOAT 428 else if (*p == 'f') 429 { 430 sort_flt = 1; 431 ++format_found; 432 } 433 #endif 434 else if (*p == 'b') 435 { 436 sort_what = STR2NR_BIN + STR2NR_FORCE; 437 ++format_found; 438 } 439 else if (*p == 'o') 440 { 441 sort_what = STR2NR_OCT + STR2NR_FORCE; 442 ++format_found; 443 } 444 else if (*p == 'x') 445 { 446 sort_what = STR2NR_HEX + STR2NR_FORCE; 447 ++format_found; 448 } 449 else if (*p == 'u') 450 unique = TRUE; 451 else if (*p == '"') // comment start 452 break; 453 else if (check_nextcmd(p) != NULL) 454 { 455 eap->nextcmd = check_nextcmd(p); 456 break; 457 } 458 else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) 459 { 460 s = skip_regexp_err(p + 1, *p, TRUE); 461 if (s == NULL) 462 goto sortend; 463 *s = NUL; 464 // Use last search pattern if sort pattern is empty. 465 if (s == p + 1) 466 { 467 if (last_search_pat() == NULL) 468 { 469 emsg(_(e_noprevre)); 470 goto sortend; 471 } 472 regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC); 473 } 474 else 475 regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC); 476 if (regmatch.regprog == NULL) 477 goto sortend; 478 p = s; // continue after the regexp 479 regmatch.rm_ic = p_ic; 480 } 481 else 482 { 483 semsg(_(e_invarg2), p); 484 goto sortend; 485 } 486 } 487 488 // Can only have one of 'n', 'b', 'o' and 'x'. 489 if (format_found > 1) 490 { 491 emsg(_(e_invarg)); 492 goto sortend; 493 } 494 495 // From here on "sort_nr" is used as a flag for any integer number 496 // sorting. 497 sort_nr += sort_what; 498 499 /* 500 * Make an array with all line numbers. This avoids having to copy all 501 * the lines into allocated memory. 502 * When sorting on strings "start_col_nr" is the offset in the line, for 503 * numbers sorting it's the number to sort on. This means the pattern 504 * matching and number conversion only has to be done once per line. 505 * Also get the longest line length for allocating "sortbuf". 506 */ 507 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) 508 { 509 s = ml_get(lnum); 510 len = (int)STRLEN(s); 511 if (maxlen < len) 512 maxlen = len; 513 514 start_col = 0; 515 end_col = len; 516 if (regmatch.regprog != NULL && vim_regexec(®match, s, 0)) 517 { 518 if (sort_rx) 519 { 520 start_col = (colnr_T)(regmatch.startp[0] - s); 521 end_col = (colnr_T)(regmatch.endp[0] - s); 522 } 523 else 524 start_col = (colnr_T)(regmatch.endp[0] - s); 525 } 526 else 527 if (regmatch.regprog != NULL) 528 end_col = 0; 529 530 if (sort_nr 531 #ifdef FEAT_FLOAT 532 || sort_flt 533 #endif 534 ) 535 { 536 // Make sure vim_str2nr doesn't read any digits past the end 537 // of the match, by temporarily terminating the string there 538 s2 = s + end_col; 539 c = *s2; 540 *s2 = NUL; 541 // Sorting on number: Store the number itself. 542 p = s + start_col; 543 if (sort_nr) 544 { 545 if (sort_what & STR2NR_HEX) 546 s = skiptohex(p); 547 else if (sort_what & STR2NR_BIN) 548 s = skiptobin(p); 549 else 550 s = skiptodigit(p); 551 if (s > p && s[-1] == '-') 552 --s; // include preceding negative sign 553 if (*s == NUL) 554 { 555 // line without number should sort before any number 556 nrs[lnum - eap->line1].st_u.num.is_number = FALSE; 557 nrs[lnum - eap->line1].st_u.num.value = 0; 558 } 559 else 560 { 561 nrs[lnum - eap->line1].st_u.num.is_number = TRUE; 562 vim_str2nr(s, NULL, NULL, sort_what, 563 &nrs[lnum - eap->line1].st_u.num.value, 564 NULL, 0, FALSE); 565 } 566 } 567 #ifdef FEAT_FLOAT 568 else 569 { 570 s = skipwhite(p); 571 if (*s == '+') 572 s = skipwhite(s + 1); 573 574 if (*s == NUL) 575 // empty line should sort before any number 576 nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX; 577 else 578 nrs[lnum - eap->line1].st_u.value_flt = 579 strtod((char *)s, NULL); 580 } 581 #endif 582 *s2 = c; 583 } 584 else 585 { 586 // Store the column to sort at. 587 nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col; 588 nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col; 589 } 590 591 nrs[lnum - eap->line1].lnum = lnum; 592 593 if (regmatch.regprog != NULL) 594 fast_breakcheck(); 595 if (got_int) 596 goto sortend; 597 } 598 599 // Allocate a buffer that can hold the longest line. 600 sortbuf1 = alloc(maxlen + 1); 601 if (sortbuf1 == NULL) 602 goto sortend; 603 sortbuf2 = alloc(maxlen + 1); 604 if (sortbuf2 == NULL) 605 goto sortend; 606 607 // Sort the array of line numbers. Note: can't be interrupted! 608 qsort((void *)nrs, count, sizeof(sorti_T), sort_compare); 609 610 if (sort_abort) 611 goto sortend; 612 613 // Insert the lines in the sorted order below the last one. 614 lnum = eap->line2; 615 for (i = 0; i < count; ++i) 616 { 617 linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum; 618 619 // If the original line number of the line being placed is not the same 620 // as "lnum" (accounting for offset), we know that the buffer changed. 621 if (get_lnum + ((linenr_T)count - 1) != lnum) 622 change_occurred = TRUE; 623 624 s = ml_get(get_lnum); 625 if (!unique || i == 0 || string_compare(s, sortbuf1) != 0) 626 { 627 // Copy the line into a buffer, it may become invalid in 628 // ml_append(). And it's needed for "unique". 629 STRCPY(sortbuf1, s); 630 if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL) 631 break; 632 } 633 fast_breakcheck(); 634 if (got_int) 635 goto sortend; 636 } 637 638 // delete the original lines if appending worked 639 if (i == count) 640 for (i = 0; i < count; ++i) 641 ml_delete(eap->line1); 642 else 643 count = 0; 644 645 // Adjust marks for deleted (or added) lines and prepare for displaying. 646 deleted = (long)(count - (lnum - eap->line2)); 647 if (deleted > 0) 648 { 649 mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted); 650 msgmore(-deleted); 651 } 652 else if (deleted < 0) 653 mark_adjust(eap->line2, MAXLNUM, -deleted, 0L); 654 655 if (change_occurred || deleted != 0) 656 changed_lines(eap->line1, 0, eap->line2 + 1, -deleted); 657 658 curwin->w_cursor.lnum = eap->line1; 659 beginline(BL_WHITE | BL_FIX); 660 661 sortend: 662 vim_free(nrs); 663 vim_free(sortbuf1); 664 vim_free(sortbuf2); 665 vim_regfree(regmatch.regprog); 666 if (got_int) 667 emsg(_(e_interr)); 668 } 669 670 /* 671 * :move command - move lines line1-line2 to line dest 672 * 673 * return FAIL for failure, OK otherwise 674 */ 675 int 676 do_move(linenr_T line1, linenr_T line2, linenr_T dest) 677 { 678 char_u *str; 679 linenr_T l; 680 linenr_T extra; // Num lines added before line1 681 linenr_T num_lines; // Num lines moved 682 linenr_T last_line; // Last line in file after adding new text 683 #ifdef FEAT_FOLDING 684 win_T *win; 685 tabpage_T *tp; 686 #endif 687 688 if (dest >= line1 && dest < line2) 689 { 690 emsg(_("E134: Cannot move a range of lines into itself")); 691 return FAIL; 692 } 693 694 // Do nothing if we are not actually moving any lines. This will prevent 695 // the 'modified' flag from being set without cause. 696 if (dest == line1 - 1 || dest == line2) 697 { 698 // Move the cursor as if lines were moved (see below) to be backwards 699 // compatible. 700 if (dest >= line1) 701 curwin->w_cursor.lnum = dest; 702 else 703 curwin->w_cursor.lnum = dest + (line2 - line1) + 1; 704 705 return OK; 706 } 707 708 num_lines = line2 - line1 + 1; 709 710 /* 711 * First we copy the old text to its new location -- webb 712 * Also copy the flag that ":global" command uses. 713 */ 714 if (u_save(dest, dest + 1) == FAIL) 715 return FAIL; 716 for (extra = 0, l = line1; l <= line2; l++) 717 { 718 str = vim_strsave(ml_get(l + extra)); 719 if (str != NULL) 720 { 721 ml_append(dest + l - line1, str, (colnr_T)0, FALSE); 722 vim_free(str); 723 if (dest < line1) 724 extra++; 725 } 726 } 727 728 /* 729 * Now we must be careful adjusting our marks so that we don't overlap our 730 * mark_adjust() calls. 731 * 732 * We adjust the marks within the old text so that they refer to the 733 * last lines of the file (temporarily), because we know no other marks 734 * will be set there since these line numbers did not exist until we added 735 * our new lines. 736 * 737 * Then we adjust the marks on lines between the old and new text positions 738 * (either forwards or backwards). 739 * 740 * And Finally we adjust the marks we put at the end of the file back to 741 * their final destination at the new text position -- webb 742 */ 743 last_line = curbuf->b_ml.ml_line_count; 744 mark_adjust_nofold(line1, line2, last_line - line2, 0L); 745 if (dest >= line2) 746 { 747 mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L); 748 #ifdef FEAT_FOLDING 749 FOR_ALL_TAB_WINDOWS(tp, win) { 750 if (win->w_buffer == curbuf) 751 foldMoveRange(&win->w_folds, line1, line2, dest); 752 } 753 #endif 754 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 755 { 756 curbuf->b_op_start.lnum = dest - num_lines + 1; 757 curbuf->b_op_end.lnum = dest; 758 } 759 } 760 else 761 { 762 mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L); 763 #ifdef FEAT_FOLDING 764 FOR_ALL_TAB_WINDOWS(tp, win) { 765 if (win->w_buffer == curbuf) 766 foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2); 767 } 768 #endif 769 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 770 { 771 curbuf->b_op_start.lnum = dest + 1; 772 curbuf->b_op_end.lnum = dest + num_lines; 773 } 774 } 775 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 776 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 777 mark_adjust_nofold(last_line - num_lines + 1, last_line, 778 -(last_line - dest - extra), 0L); 779 780 /* 781 * Now we delete the original text -- webb 782 */ 783 if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL) 784 return FAIL; 785 786 for (l = line1; l <= line2; l++) 787 ml_delete_flags(line1 + extra, ML_DEL_MESSAGE); 788 789 if (!global_busy && num_lines > p_report) 790 smsg(NGETTEXT("%ld line moved", "%ld lines moved", num_lines), 791 (long)num_lines); 792 793 /* 794 * Leave the cursor on the last of the moved lines. 795 */ 796 if (dest >= line1) 797 curwin->w_cursor.lnum = dest; 798 else 799 curwin->w_cursor.lnum = dest + (line2 - line1) + 1; 800 801 if (line1 < dest) 802 { 803 dest += num_lines + 1; 804 last_line = curbuf->b_ml.ml_line_count; 805 if (dest > last_line + 1) 806 dest = last_line + 1; 807 changed_lines(line1, 0, dest, 0L); 808 } 809 else 810 changed_lines(dest + 1, 0, line1 + num_lines, 0L); 811 812 return OK; 813 } 814 815 /* 816 * ":copy" 817 */ 818 void 819 ex_copy(linenr_T line1, linenr_T line2, linenr_T n) 820 { 821 linenr_T count; 822 char_u *p; 823 824 count = line2 - line1 + 1; 825 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 826 { 827 curbuf->b_op_start.lnum = n + 1; 828 curbuf->b_op_end.lnum = n + count; 829 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 830 } 831 832 /* 833 * there are three situations: 834 * 1. destination is above line1 835 * 2. destination is between line1 and line2 836 * 3. destination is below line2 837 * 838 * n = destination (when starting) 839 * curwin->w_cursor.lnum = destination (while copying) 840 * line1 = start of source (while copying) 841 * line2 = end of source (while copying) 842 */ 843 if (u_save(n, n + 1) == FAIL) 844 return; 845 846 curwin->w_cursor.lnum = n; 847 while (line1 <= line2) 848 { 849 // need to use vim_strsave() because the line will be unlocked within 850 // ml_append() 851 p = vim_strsave(ml_get(line1)); 852 if (p != NULL) 853 { 854 ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE); 855 vim_free(p); 856 } 857 // situation 2: skip already copied lines 858 if (line1 == n) 859 line1 = curwin->w_cursor.lnum; 860 ++line1; 861 if (curwin->w_cursor.lnum < line1) 862 ++line1; 863 if (curwin->w_cursor.lnum < line2) 864 ++line2; 865 ++curwin->w_cursor.lnum; 866 } 867 868 appended_lines_mark(n, count); 869 870 msgmore((long)count); 871 } 872 873 static char_u *prevcmd = NULL; // the previous command 874 875 #if defined(EXITFREE) || defined(PROTO) 876 void 877 free_prev_shellcmd(void) 878 { 879 vim_free(prevcmd); 880 } 881 #endif 882 883 /* 884 * Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd" 885 * Bangs in the argument are replaced with the previously entered command. 886 * Remember the argument. 887 */ 888 void 889 do_bang( 890 int addr_count, 891 exarg_T *eap, 892 int forceit, 893 int do_in, 894 int do_out) 895 { 896 char_u *arg = eap->arg; // command 897 linenr_T line1 = eap->line1; // start of range 898 linenr_T line2 = eap->line2; // end of range 899 char_u *newcmd = NULL; // the new command 900 int free_newcmd = FALSE; // need to free() newcmd 901 int ins_prevcmd; 902 char_u *t; 903 char_u *p; 904 char_u *trailarg; 905 int len; 906 int scroll_save = msg_scroll; 907 908 /* 909 * Disallow shell commands for "rvim". 910 * Disallow shell commands from .exrc and .vimrc in current directory for 911 * security reasons. 912 */ 913 if (check_restricted() || check_secure()) 914 return; 915 916 if (addr_count == 0) // :! 917 { 918 msg_scroll = FALSE; // don't scroll here 919 autowrite_all(); 920 msg_scroll = scroll_save; 921 } 922 923 /* 924 * Try to find an embedded bang, like in :!<cmd> ! [args] 925 * (:!! is indicated by the 'forceit' variable) 926 */ 927 ins_prevcmd = forceit; 928 trailarg = arg; 929 do 930 { 931 len = (int)STRLEN(trailarg) + 1; 932 if (newcmd != NULL) 933 len += (int)STRLEN(newcmd); 934 if (ins_prevcmd) 935 { 936 if (prevcmd == NULL) 937 { 938 emsg(_(e_noprev)); 939 vim_free(newcmd); 940 return; 941 } 942 len += (int)STRLEN(prevcmd); 943 } 944 if ((t = alloc(len)) == NULL) 945 { 946 vim_free(newcmd); 947 return; 948 } 949 *t = NUL; 950 if (newcmd != NULL) 951 STRCAT(t, newcmd); 952 if (ins_prevcmd) 953 STRCAT(t, prevcmd); 954 p = t + STRLEN(t); 955 STRCAT(t, trailarg); 956 vim_free(newcmd); 957 newcmd = t; 958 959 /* 960 * Scan the rest of the argument for '!', which is replaced by the 961 * previous command. "\!" is replaced by "!" (this is vi compatible). 962 */ 963 trailarg = NULL; 964 while (*p) 965 { 966 if (*p == '!') 967 { 968 if (p > newcmd && p[-1] == '\\') 969 STRMOVE(p - 1, p); 970 else 971 { 972 trailarg = p; 973 *trailarg++ = NUL; 974 ins_prevcmd = TRUE; 975 break; 976 } 977 } 978 ++p; 979 } 980 } while (trailarg != NULL); 981 982 vim_free(prevcmd); 983 prevcmd = newcmd; 984 985 if (bangredo) // put cmd in redo buffer for ! command 986 { 987 // If % or # appears in the command, it must have been escaped. 988 // Reescape them, so that redoing them does not substitute them by the 989 // buffername. 990 char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#"); 991 992 if (cmd != NULL) 993 { 994 AppendToRedobuffLit(cmd, -1); 995 vim_free(cmd); 996 } 997 else 998 AppendToRedobuffLit(prevcmd, -1); 999 AppendToRedobuff((char_u *)"\n"); 1000 bangredo = FALSE; 1001 } 1002 /* 1003 * Add quotes around the command, for shells that need them. 1004 */ 1005 if (*p_shq != NUL) 1006 { 1007 newcmd = alloc(STRLEN(prevcmd) + 2 * STRLEN(p_shq) + 1); 1008 if (newcmd == NULL) 1009 return; 1010 STRCPY(newcmd, p_shq); 1011 STRCAT(newcmd, prevcmd); 1012 STRCAT(newcmd, p_shq); 1013 free_newcmd = TRUE; 1014 } 1015 if (addr_count == 0) // :! 1016 { 1017 // echo the command 1018 msg_start(); 1019 msg_putchar(':'); 1020 msg_putchar('!'); 1021 msg_outtrans(newcmd); 1022 msg_clr_eos(); 1023 windgoto(msg_row, msg_col); 1024 1025 do_shell(newcmd, 0); 1026 } 1027 else // :range! 1028 { 1029 // Careful: This may recursively call do_bang() again! (because of 1030 // autocommands) 1031 do_filter(line1, line2, eap, newcmd, do_in, do_out); 1032 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); 1033 } 1034 if (free_newcmd) 1035 vim_free(newcmd); 1036 } 1037 1038 /* 1039 * do_filter: filter lines through a command given by the user 1040 * 1041 * We mostly use temp files and the call_shell() routine here. This would 1042 * normally be done using pipes on a UNIX machine, but this is more portable 1043 * to non-unix machines. The call_shell() routine needs to be able 1044 * to deal with redirection somehow, and should handle things like looking 1045 * at the PATH env. variable, and adding reasonable extensions to the 1046 * command name given by the user. All reasonable versions of call_shell() 1047 * do this. 1048 * Alternatively, if on Unix and redirecting input or output, but not both, 1049 * and the 'shelltemp' option isn't set, use pipes. 1050 * We use input redirection if do_in is TRUE. 1051 * We use output redirection if do_out is TRUE. 1052 */ 1053 static void 1054 do_filter( 1055 linenr_T line1, 1056 linenr_T line2, 1057 exarg_T *eap, // for forced 'ff' and 'fenc' 1058 char_u *cmd, 1059 int do_in, 1060 int do_out) 1061 { 1062 char_u *itmp = NULL; 1063 char_u *otmp = NULL; 1064 linenr_T linecount; 1065 linenr_T read_linecount; 1066 pos_T cursor_save; 1067 char_u *cmd_buf; 1068 buf_T *old_curbuf = curbuf; 1069 int shell_flags = 0; 1070 pos_T orig_start = curbuf->b_op_start; 1071 pos_T orig_end = curbuf->b_op_end; 1072 int save_cmod_flags = cmdmod.cmod_flags; 1073 #ifdef FEAT_FILTERPIPE 1074 int stmp = p_stmp; 1075 #endif 1076 1077 if (*cmd == NUL) // no filter command 1078 return; 1079 1080 // Temporarily disable lockmarks since that's needed to propagate changed 1081 // regions of the buffer for foldUpdate(), linecount, etc. 1082 cmdmod.cmod_flags &= ~CMOD_LOCKMARKS; 1083 1084 cursor_save = curwin->w_cursor; 1085 linecount = line2 - line1 + 1; 1086 curwin->w_cursor.lnum = line1; 1087 curwin->w_cursor.col = 0; 1088 changed_line_abv_curs(); 1089 invalidate_botline(); 1090 1091 /* 1092 * When using temp files: 1093 * 1. * Form temp file names 1094 * 2. * Write the lines to a temp file 1095 * 3. Run the filter command on the temp file 1096 * 4. * Read the output of the command into the buffer 1097 * 5. * Delete the original lines to be filtered 1098 * 6. * Remove the temp files 1099 * 1100 * When writing the input with a pipe or when catching the output with a 1101 * pipe only need to do 3. 1102 */ 1103 1104 if (do_out) 1105 shell_flags |= SHELL_DOOUT; 1106 1107 #ifdef FEAT_FILTERPIPE 1108 # ifdef VIMDLL 1109 if (!gui.in_use && !gui.starting) 1110 stmp = 1; // Console mode doesn't support filterpipe. 1111 # endif 1112 1113 if (!do_in && do_out && !stmp) 1114 { 1115 // Use a pipe to fetch stdout of the command, do not use a temp file. 1116 shell_flags |= SHELL_READ; 1117 curwin->w_cursor.lnum = line2; 1118 } 1119 else if (do_in && !do_out && !stmp) 1120 { 1121 // Use a pipe to write stdin of the command, do not use a temp file. 1122 shell_flags |= SHELL_WRITE; 1123 curbuf->b_op_start.lnum = line1; 1124 curbuf->b_op_end.lnum = line2; 1125 } 1126 else if (do_in && do_out && !stmp) 1127 { 1128 // Use a pipe to write stdin and fetch stdout of the command, do not 1129 // use a temp file. 1130 shell_flags |= SHELL_READ|SHELL_WRITE; 1131 curbuf->b_op_start.lnum = line1; 1132 curbuf->b_op_end.lnum = line2; 1133 curwin->w_cursor.lnum = line2; 1134 } 1135 else 1136 #endif 1137 if ((do_in && (itmp = vim_tempname('i', FALSE)) == NULL) 1138 || (do_out && (otmp = vim_tempname('o', FALSE)) == NULL)) 1139 { 1140 emsg(_(e_notmp)); 1141 goto filterend; 1142 } 1143 1144 /* 1145 * The writing and reading of temp files will not be shown. 1146 * Vi also doesn't do this and the messages are not very informative. 1147 */ 1148 ++no_wait_return; // don't call wait_return() while busy 1149 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, 1150 FALSE, FALSE, FALSE, TRUE) == FAIL) 1151 { 1152 msg_putchar('\n'); // keep message from buf_write() 1153 --no_wait_return; 1154 #if defined(FEAT_EVAL) 1155 if (!aborting()) 1156 #endif 1157 (void)semsg(_(e_notcreate), itmp); // will call wait_return 1158 goto filterend; 1159 } 1160 if (curbuf != old_curbuf) 1161 goto filterend; 1162 1163 if (!do_out) 1164 msg_putchar('\n'); 1165 1166 // Create the shell command in allocated memory. 1167 cmd_buf = make_filter_cmd(cmd, itmp, otmp); 1168 if (cmd_buf == NULL) 1169 goto filterend; 1170 1171 windgoto((int)Rows - 1, 0); 1172 cursor_on(); 1173 1174 /* 1175 * When not redirecting the output the command can write anything to the 1176 * screen. If 'shellredir' is equal to ">", screen may be messed up by 1177 * stderr output of external command. Clear the screen later. 1178 * If do_in is FALSE, this could be something like ":r !cat", which may 1179 * also mess up the screen, clear it later. 1180 */ 1181 if (!do_out || STRCMP(p_srr, ">") == 0 || !do_in) 1182 redraw_later_clear(); 1183 1184 if (do_out) 1185 { 1186 if (u_save((linenr_T)(line2), (linenr_T)(line2 + 1)) == FAIL) 1187 { 1188 vim_free(cmd_buf); 1189 goto error; 1190 } 1191 redraw_curbuf_later(VALID); 1192 } 1193 read_linecount = curbuf->b_ml.ml_line_count; 1194 1195 /* 1196 * When call_shell() fails wait_return() is called to give the user a 1197 * chance to read the error messages. Otherwise errors are ignored, so you 1198 * can see the error messages from the command that appear on stdout; use 1199 * 'u' to fix the text 1200 * Switch to cooked mode when not redirecting stdin, avoids that something 1201 * like ":r !cat" hangs. 1202 * Pass on the SHELL_DOOUT flag when the output is being redirected. 1203 */ 1204 if (call_shell(cmd_buf, SHELL_FILTER | SHELL_COOKED | shell_flags)) 1205 { 1206 redraw_later_clear(); 1207 wait_return(FALSE); 1208 } 1209 vim_free(cmd_buf); 1210 1211 did_check_timestamps = FALSE; 1212 need_check_timestamps = TRUE; 1213 1214 // When interrupting the shell command, it may still have produced some 1215 // useful output. Reset got_int here, so that readfile() won't cancel 1216 // reading. 1217 ui_breakcheck(); 1218 got_int = FALSE; 1219 1220 if (do_out) 1221 { 1222 if (otmp != NULL) 1223 { 1224 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, 1225 eap, READ_FILTER) != OK) 1226 { 1227 #if defined(FEAT_EVAL) 1228 if (!aborting()) 1229 #endif 1230 { 1231 msg_putchar('\n'); 1232 semsg(_(e_notread), otmp); 1233 } 1234 goto error; 1235 } 1236 if (curbuf != old_curbuf) 1237 goto filterend; 1238 } 1239 1240 read_linecount = curbuf->b_ml.ml_line_count - read_linecount; 1241 1242 if (shell_flags & SHELL_READ) 1243 { 1244 curbuf->b_op_start.lnum = line2 + 1; 1245 curbuf->b_op_end.lnum = curwin->w_cursor.lnum; 1246 appended_lines_mark(line2, read_linecount); 1247 } 1248 1249 if (do_in) 1250 { 1251 if ((cmdmod.cmod_flags & CMOD_KEEPMARKS) 1252 || vim_strchr(p_cpo, CPO_REMMARK) == NULL) 1253 { 1254 if (read_linecount >= linecount) 1255 // move all marks from old lines to new lines 1256 mark_adjust(line1, line2, linecount, 0L); 1257 else if (save_cmod_flags & CMOD_LOCKMARKS) 1258 { 1259 // Move marks from the lines below the new lines down by 1260 // the number of lines lost. 1261 // Move marks from the lines that will be deleted to the 1262 // new lines and below. 1263 mark_adjust(line2 + 1, (linenr_T)MAXLNUM, 1264 linecount - read_linecount, 0L); 1265 mark_adjust(line1, line2, linecount, 0L); 1266 } 1267 else 1268 { 1269 // move marks from old lines to new lines, delete marks 1270 // that are in deleted lines 1271 mark_adjust(line1, line1 + read_linecount - 1, 1272 linecount, 0L); 1273 mark_adjust(line1 + read_linecount, line2, MAXLNUM, 0L); 1274 } 1275 } 1276 1277 /* 1278 * Put cursor on first filtered line for ":range!cmd". 1279 * Adjust '[ and '] (set by buf_write()). 1280 */ 1281 curwin->w_cursor.lnum = line1; 1282 del_lines(linecount, TRUE); 1283 curbuf->b_op_start.lnum -= linecount; // adjust '[ 1284 curbuf->b_op_end.lnum -= linecount; // adjust '] 1285 write_lnum_adjust(-linecount); // adjust last line 1286 // for next write 1287 #ifdef FEAT_FOLDING 1288 foldUpdate(curwin, curbuf->b_op_start.lnum, curbuf->b_op_end.lnum); 1289 #endif 1290 } 1291 else 1292 { 1293 /* 1294 * Put cursor on last new line for ":r !cmd". 1295 */ 1296 linecount = curbuf->b_op_end.lnum - curbuf->b_op_start.lnum + 1; 1297 curwin->w_cursor.lnum = curbuf->b_op_end.lnum; 1298 } 1299 1300 beginline(BL_WHITE | BL_FIX); // cursor on first non-blank 1301 --no_wait_return; 1302 1303 if (linecount > p_report) 1304 { 1305 if (do_in) 1306 { 1307 vim_snprintf(msg_buf, sizeof(msg_buf), 1308 _("%ld lines filtered"), (long)linecount); 1309 if (msg(msg_buf) && !msg_scroll) 1310 // save message to display it after redraw 1311 set_keep_msg((char_u *)msg_buf, 0); 1312 } 1313 else 1314 msgmore((long)linecount); 1315 } 1316 } 1317 else 1318 { 1319 error: 1320 // put cursor back in same position for ":w !cmd" 1321 curwin->w_cursor = cursor_save; 1322 --no_wait_return; 1323 wait_return(FALSE); 1324 } 1325 1326 filterend: 1327 1328 cmdmod.cmod_flags = save_cmod_flags; 1329 if (curbuf != old_curbuf) 1330 { 1331 --no_wait_return; 1332 emsg(_("E135: *Filter* Autocommands must not change current buffer")); 1333 } 1334 else if (cmdmod.cmod_flags & CMOD_LOCKMARKS) 1335 { 1336 curbuf->b_op_start = orig_start; 1337 curbuf->b_op_end = orig_end; 1338 } 1339 1340 if (itmp != NULL) 1341 mch_remove(itmp); 1342 if (otmp != NULL) 1343 mch_remove(otmp); 1344 vim_free(itmp); 1345 vim_free(otmp); 1346 } 1347 1348 /* 1349 * Call a shell to execute a command. 1350 * When "cmd" is NULL start an interactive shell. 1351 */ 1352 void 1353 do_shell( 1354 char_u *cmd, 1355 int flags) // may be SHELL_DOOUT when output is redirected 1356 { 1357 buf_T *buf; 1358 #if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) 1359 int save_nwr; 1360 #endif 1361 #ifdef MSWIN 1362 int winstart = FALSE; 1363 #endif 1364 int keep_termcap = !termcap_active; 1365 1366 /* 1367 * Disallow shell commands for "rvim". 1368 * Disallow shell commands from .exrc and .vimrc in current directory for 1369 * security reasons. 1370 */ 1371 if (check_restricted() || check_secure()) 1372 { 1373 msg_end(); 1374 return; 1375 } 1376 1377 #ifdef MSWIN 1378 /* 1379 * Check if ":!start" is used. This implies not stopping termcap mode. 1380 */ 1381 if (cmd != NULL) 1382 keep_termcap = winstart = (STRNICMP(cmd, "start ", 6) == 0); 1383 1384 # if defined(FEAT_GUI) && defined(FEAT_TERMINAL) 1385 // Don't stop termcap mode when using a terminal window for the shell. 1386 if (gui.in_use && vim_strchr(p_go, GO_TERMINAL) != NULL) 1387 keep_termcap = TRUE; 1388 # endif 1389 #endif 1390 1391 /* 1392 * For autocommands we want to get the output on the current screen, to 1393 * avoid having to type return below. 1394 */ 1395 msg_putchar('\r'); // put cursor at start of line 1396 if (!autocmd_busy) 1397 { 1398 if (!keep_termcap) 1399 stoptermcap(); 1400 } 1401 #ifdef MSWIN 1402 if (!winstart) 1403 #endif 1404 msg_putchar('\n'); // may shift screen one line up 1405 1406 // warning message before calling the shell 1407 if (p_warn && !autocmd_busy && msg_silent == 0) 1408 FOR_ALL_BUFFERS(buf) 1409 if (bufIsChangedNotTerm(buf)) 1410 { 1411 #ifdef FEAT_GUI_MSWIN 1412 if (!keep_termcap) 1413 starttermcap(); // don't want a message box here 1414 #endif 1415 msg_puts(_("[No write since last change]\n")); 1416 #ifdef FEAT_GUI_MSWIN 1417 if (!keep_termcap) 1418 stoptermcap(); 1419 #endif 1420 break; 1421 } 1422 1423 // This windgoto is required for when the '\n' resulted in a "delete line 1424 // 1" command to the terminal. 1425 if (!swapping_screen()) 1426 windgoto(msg_row, msg_col); 1427 cursor_on(); 1428 (void)call_shell(cmd, SHELL_COOKED | flags); 1429 did_check_timestamps = FALSE; 1430 need_check_timestamps = TRUE; 1431 1432 /* 1433 * put the message cursor at the end of the screen, avoids wait_return() 1434 * to overwrite the text that the external command showed 1435 */ 1436 if (!swapping_screen()) 1437 { 1438 msg_row = Rows - 1; 1439 msg_col = 0; 1440 } 1441 1442 if (autocmd_busy) 1443 { 1444 if (msg_silent == 0) 1445 redraw_later_clear(); 1446 } 1447 else 1448 { 1449 /* 1450 * For ":sh" there is no need to call wait_return(), just redraw. 1451 * Also for the Win32 GUI (the output is in a console window). 1452 * Otherwise there is probably text on the screen that the user wants 1453 * to read before redrawing, so call wait_return(). 1454 */ 1455 #if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL) 1456 # ifdef VIMDLL 1457 if (!gui.in_use) 1458 # endif 1459 { 1460 if (cmd == NULL 1461 # ifdef MSWIN 1462 || (keep_termcap && !need_wait_return) 1463 # endif 1464 ) 1465 { 1466 if (msg_silent == 0) 1467 redraw_later_clear(); 1468 need_wait_return = FALSE; 1469 } 1470 else 1471 { 1472 /* 1473 * If we switch screens when starttermcap() is called, we 1474 * really want to wait for "hit return to continue". 1475 */ 1476 save_nwr = no_wait_return; 1477 if (swapping_screen()) 1478 no_wait_return = FALSE; 1479 # ifdef AMIGA 1480 wait_return(term_console ? -1 : msg_silent == 0); // see below 1481 # else 1482 wait_return(msg_silent == 0); 1483 # endif 1484 no_wait_return = save_nwr; 1485 } 1486 } 1487 #endif // FEAT_GUI_MSWIN 1488 1489 if (!keep_termcap) // if keep_termcap is TRUE didn't stop termcap 1490 starttermcap(); // start termcap if not done by wait_return() 1491 1492 /* 1493 * In an Amiga window redrawing is caused by asking the window size. 1494 * If we got an interrupt this will not work. The chance that the 1495 * window size is wrong is very small, but we need to redraw the 1496 * screen. Don't do this if ':' hit in wait_return(). THIS IS UGLY 1497 * but it saves an extra redraw. 1498 */ 1499 #ifdef AMIGA 1500 if (skip_redraw) // ':' hit in wait_return() 1501 { 1502 if (msg_silent == 0) 1503 redraw_later_clear(); 1504 } 1505 else if (term_console) 1506 { 1507 OUT_STR(IF_EB("\033[0 q", ESC_STR "[0 q")); // get window size 1508 if (got_int && msg_silent == 0) 1509 redraw_later_clear(); // if got_int is TRUE, redraw needed 1510 else 1511 must_redraw = 0; // no extra redraw needed 1512 } 1513 #endif 1514 } 1515 1516 // display any error messages now 1517 display_errors(); 1518 1519 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); 1520 } 1521 1522 #if !defined(UNIX) 1523 static char_u * 1524 find_pipe(char_u *cmd) 1525 { 1526 char_u *p; 1527 int inquote = FALSE; 1528 1529 for (p = cmd; *p != NUL; ++p) 1530 { 1531 if (!inquote && *p == '|') 1532 return p; 1533 if (*p == '"') 1534 inquote = !inquote; 1535 else if (rem_backslash(p)) 1536 ++p; 1537 } 1538 return NULL; 1539 } 1540 #endif 1541 1542 /* 1543 * Create a shell command from a command string, input redirection file and 1544 * output redirection file. 1545 * Returns an allocated string with the shell command, or NULL for failure. 1546 */ 1547 char_u * 1548 make_filter_cmd( 1549 char_u *cmd, // command 1550 char_u *itmp, // NULL or name of input file 1551 char_u *otmp) // NULL or name of output file 1552 { 1553 char_u *buf; 1554 long_u len; 1555 1556 #if defined(UNIX) 1557 int is_fish_shell; 1558 char_u *shell_name = get_isolated_shell_name(); 1559 1560 if (shell_name == NULL) 1561 return NULL; 1562 1563 // Account for fish's different syntax for subshells 1564 is_fish_shell = (fnamecmp(shell_name, "fish") == 0); 1565 vim_free(shell_name); 1566 if (is_fish_shell) 1567 len = (long_u)STRLEN(cmd) + 13; // "begin; " + "; end" + NUL 1568 else 1569 #endif 1570 len = (long_u)STRLEN(cmd) + 3; // "()" + NUL 1571 if (itmp != NULL) 1572 len += (long_u)STRLEN(itmp) + 9; // " { < " + " } " 1573 if (otmp != NULL) 1574 len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; // " " 1575 buf = alloc(len); 1576 if (buf == NULL) 1577 return NULL; 1578 1579 #if defined(UNIX) 1580 /* 1581 * Put braces around the command (for concatenated commands) when 1582 * redirecting input and/or output. 1583 */ 1584 if (itmp != NULL || otmp != NULL) 1585 { 1586 if (is_fish_shell) 1587 vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); 1588 else 1589 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); 1590 } 1591 else 1592 STRCPY(buf, cmd); 1593 if (itmp != NULL) 1594 { 1595 STRCAT(buf, " < "); 1596 STRCAT(buf, itmp); 1597 } 1598 #else 1599 // For shells that don't understand braces around commands, at least allow 1600 // the use of commands in a pipe. 1601 if (*p_sxe != NUL && *p_sxq == '(') 1602 { 1603 if (itmp != NULL || otmp != NULL) 1604 vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); 1605 else 1606 STRCPY(buf, cmd); 1607 if (itmp != NULL) 1608 { 1609 STRCAT(buf, " < "); 1610 STRCAT(buf, itmp); 1611 } 1612 } 1613 else 1614 { 1615 STRCPY(buf, cmd); 1616 if (itmp != NULL) 1617 { 1618 char_u *p; 1619 1620 // If there is a pipe, we have to put the '<' in front of it. 1621 // Don't do this when 'shellquote' is not empty, otherwise the 1622 // redirection would be inside the quotes. 1623 if (*p_shq == NUL) 1624 { 1625 p = find_pipe(buf); 1626 if (p != NULL) 1627 *p = NUL; 1628 } 1629 STRCAT(buf, " <"); // " < " causes problems on Amiga 1630 STRCAT(buf, itmp); 1631 if (*p_shq == NUL) 1632 { 1633 p = find_pipe(cmd); 1634 if (p != NULL) 1635 { 1636 STRCAT(buf, " "); // insert a space before the '|' for DOS 1637 STRCAT(buf, p); 1638 } 1639 } 1640 } 1641 } 1642 #endif 1643 if (otmp != NULL) 1644 append_redir(buf, (int)len, p_srr, otmp); 1645 1646 return buf; 1647 } 1648 1649 /* 1650 * Append output redirection for file "fname" to the end of string buffer 1651 * "buf[buflen]" 1652 * Works with the 'shellredir' and 'shellpipe' options. 1653 * The caller should make sure that there is enough room: 1654 * STRLEN(opt) + STRLEN(fname) + 3 1655 */ 1656 void 1657 append_redir( 1658 char_u *buf, 1659 int buflen, 1660 char_u *opt, 1661 char_u *fname) 1662 { 1663 char_u *p; 1664 char_u *end; 1665 1666 end = buf + STRLEN(buf); 1667 // find "%s" 1668 for (p = opt; (p = vim_strchr(p, '%')) != NULL; ++p) 1669 { 1670 if (p[1] == 's') // found %s 1671 break; 1672 if (p[1] == '%') // skip %% 1673 ++p; 1674 } 1675 if (p != NULL) 1676 { 1677 #ifdef MSWIN 1678 *end++ = ' '; // not really needed? Not with sh, ksh or bash 1679 #endif 1680 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), 1681 (char *)opt, (char *)fname); 1682 } 1683 else 1684 vim_snprintf((char *)end, (size_t)(buflen - (end - buf)), 1685 #ifdef FEAT_QUICKFIX 1686 " %s %s", 1687 #else 1688 " %s%s", // " > %s" causes problems on Amiga 1689 #endif 1690 (char *)opt, (char *)fname); 1691 } 1692 1693 /* 1694 * Implementation of ":fixdel", also used by get_stty(). 1695 * <BS> resulting <Del> 1696 * ^? ^H 1697 * not ^? ^? 1698 */ 1699 void 1700 do_fixdel(exarg_T *eap UNUSED) 1701 { 1702 char_u *p; 1703 1704 p = find_termcode((char_u *)"kb"); 1705 add_termcode((char_u *)"kD", p != NULL 1706 && *p == DEL ? (char_u *)CTRL_H_STR : DEL_STR, FALSE); 1707 } 1708 1709 void 1710 print_line_no_prefix( 1711 linenr_T lnum, 1712 int use_number, 1713 int list) 1714 { 1715 char numbuf[30]; 1716 1717 if (curwin->w_p_nu || use_number) 1718 { 1719 vim_snprintf(numbuf, sizeof(numbuf), 1720 "%*ld ", number_width(curwin), (long)lnum); 1721 msg_puts_attr(numbuf, HL_ATTR(HLF_N)); // Highlight line nrs 1722 } 1723 msg_prt_line(ml_get(lnum), list); 1724 } 1725 1726 /* 1727 * Print a text line. Also in silent mode ("ex -s"). 1728 */ 1729 void 1730 print_line(linenr_T lnum, int use_number, int list) 1731 { 1732 int save_silent = silent_mode; 1733 1734 // apply :filter /pat/ 1735 if (message_filtered(ml_get(lnum))) 1736 return; 1737 1738 msg_start(); 1739 silent_mode = FALSE; 1740 info_message = TRUE; // use mch_msg(), not mch_errmsg() 1741 print_line_no_prefix(lnum, use_number, list); 1742 if (save_silent) 1743 { 1744 msg_putchar('\n'); 1745 cursor_on(); // msg_start() switches it off 1746 out_flush(); 1747 silent_mode = save_silent; 1748 } 1749 info_message = FALSE; 1750 } 1751 1752 int 1753 rename_buffer(char_u *new_fname) 1754 { 1755 char_u *fname, *sfname, *xfname; 1756 buf_T *buf; 1757 1758 buf = curbuf; 1759 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 1760 // buffer changed, don't change name now 1761 if (buf != curbuf) 1762 return FAIL; 1763 #ifdef FEAT_EVAL 1764 if (aborting()) // autocmds may abort script processing 1765 return FAIL; 1766 #endif 1767 /* 1768 * The name of the current buffer will be changed. 1769 * A new (unlisted) buffer entry needs to be made to hold the old file 1770 * name, which will become the alternate file name. 1771 * But don't set the alternate file name if the buffer didn't have a 1772 * name. 1773 */ 1774 fname = curbuf->b_ffname; 1775 sfname = curbuf->b_sfname; 1776 xfname = curbuf->b_fname; 1777 curbuf->b_ffname = NULL; 1778 curbuf->b_sfname = NULL; 1779 if (setfname(curbuf, new_fname, NULL, TRUE) == FAIL) 1780 { 1781 curbuf->b_ffname = fname; 1782 curbuf->b_sfname = sfname; 1783 return FAIL; 1784 } 1785 curbuf->b_flags |= BF_NOTEDITED; 1786 if (xfname != NULL && *xfname != NUL) 1787 { 1788 buf = buflist_new(fname, xfname, curwin->w_cursor.lnum, 0); 1789 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0) 1790 curwin->w_alt_fnum = buf->b_fnum; 1791 } 1792 vim_free(fname); 1793 vim_free(sfname); 1794 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 1795 1796 // Change directories when the 'acd' option is set. 1797 DO_AUTOCHDIR; 1798 return OK; 1799 } 1800 1801 /* 1802 * ":file[!] [fname]". 1803 */ 1804 void 1805 ex_file(exarg_T *eap) 1806 { 1807 // ":0file" removes the file name. Check for illegal uses ":3file", 1808 // "0file name", etc. 1809 if (eap->addr_count > 0 1810 && (*eap->arg != NUL 1811 || eap->line2 > 0 1812 || eap->addr_count > 1)) 1813 { 1814 emsg(_(e_invarg)); 1815 return; 1816 } 1817 1818 if (*eap->arg != NUL || eap->addr_count == 1) 1819 { 1820 if (rename_buffer(eap->arg) == FAIL) 1821 return; 1822 redraw_tabline = TRUE; 1823 } 1824 1825 // print file name if no argument or 'F' is not in 'shortmess' 1826 if (*eap->arg == NUL || !shortmess(SHM_FILEINFO)) 1827 fileinfo(FALSE, FALSE, eap->forceit); 1828 } 1829 1830 /* 1831 * ":update". 1832 */ 1833 void 1834 ex_update(exarg_T *eap) 1835 { 1836 if (curbufIsChanged()) 1837 (void)do_write(eap); 1838 } 1839 1840 /* 1841 * ":write" and ":saveas". 1842 */ 1843 void 1844 ex_write(exarg_T *eap) 1845 { 1846 if (eap->cmdidx == CMD_saveas) 1847 { 1848 // :saveas does not take a range, uses all lines. 1849 eap->line1 = 1; 1850 eap->line2 = curbuf->b_ml.ml_line_count; 1851 } 1852 1853 if (eap->usefilter) // input lines to shell command 1854 do_bang(1, eap, FALSE, TRUE, FALSE); 1855 else 1856 (void)do_write(eap); 1857 } 1858 1859 /* 1860 * write current buffer to file 'eap->arg' 1861 * if 'eap->append' is TRUE, append to the file 1862 * 1863 * if *eap->arg == NUL write to current file 1864 * 1865 * return FAIL for failure, OK otherwise 1866 */ 1867 int 1868 do_write(exarg_T *eap) 1869 { 1870 int other; 1871 char_u *fname = NULL; // init to shut up gcc 1872 char_u *ffname; 1873 int retval = FAIL; 1874 char_u *free_fname = NULL; 1875 #ifdef FEAT_BROWSE 1876 char_u *browse_file = NULL; 1877 #endif 1878 buf_T *alt_buf = NULL; 1879 int name_was_missing; 1880 1881 if (not_writing()) // check 'write' option 1882 return FAIL; 1883 1884 ffname = eap->arg; 1885 #ifdef FEAT_BROWSE 1886 if ((cmdmod.cmod_flags & CMOD_BROWSE) && !exiting) 1887 { 1888 browse_file = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), ffname, 1889 NULL, NULL, NULL, curbuf); 1890 if (browse_file == NULL) 1891 goto theend; 1892 ffname = browse_file; 1893 } 1894 #endif 1895 if (*ffname == NUL) 1896 { 1897 if (eap->cmdidx == CMD_saveas) 1898 { 1899 emsg(_(e_argreq)); 1900 goto theend; 1901 } 1902 other = FALSE; 1903 } 1904 else 1905 { 1906 fname = ffname; 1907 free_fname = fix_fname(ffname); 1908 /* 1909 * When out-of-memory, keep unexpanded file name, because we MUST be 1910 * able to write the file in this situation. 1911 */ 1912 if (free_fname != NULL) 1913 ffname = free_fname; 1914 other = otherfile(ffname); 1915 } 1916 1917 /* 1918 * If we have a new file, put its name in the list of alternate file names. 1919 */ 1920 if (other) 1921 { 1922 if (vim_strchr(p_cpo, CPO_ALTWRITE) != NULL 1923 || eap->cmdidx == CMD_saveas) 1924 alt_buf = setaltfname(ffname, fname, (linenr_T)1); 1925 else 1926 alt_buf = buflist_findname(ffname); 1927 if (alt_buf != NULL && alt_buf->b_ml.ml_mfp != NULL) 1928 { 1929 // Overwriting a file that is loaded in another buffer is not a 1930 // good idea. 1931 emsg(_(e_bufloaded)); 1932 goto theend; 1933 } 1934 } 1935 1936 /* 1937 * Writing to the current file is not allowed in readonly mode 1938 * and a file name is required. 1939 * "nofile" and "nowrite" buffers cannot be written implicitly either. 1940 */ 1941 if (!other && ( 1942 #ifdef FEAT_QUICKFIX 1943 bt_dontwrite_msg(curbuf) || 1944 #endif 1945 check_fname() == FAIL || check_readonly(&eap->forceit, curbuf))) 1946 goto theend; 1947 1948 if (!other) 1949 { 1950 ffname = curbuf->b_ffname; 1951 fname = curbuf->b_fname; 1952 /* 1953 * Not writing the whole file is only allowed with '!'. 1954 */ 1955 if ( (eap->line1 != 1 1956 || eap->line2 != curbuf->b_ml.ml_line_count) 1957 && !eap->forceit 1958 && !eap->append 1959 && !p_wa) 1960 { 1961 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 1962 if (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) 1963 { 1964 if (vim_dialog_yesno(VIM_QUESTION, NULL, 1965 (char_u *)_("Write partial file?"), 2) != VIM_YES) 1966 goto theend; 1967 eap->forceit = TRUE; 1968 } 1969 else 1970 #endif 1971 { 1972 emsg(_("E140: Use ! to write partial buffer")); 1973 goto theend; 1974 } 1975 } 1976 } 1977 1978 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) 1979 { 1980 if (eap->cmdidx == CMD_saveas && alt_buf != NULL) 1981 { 1982 buf_T *was_curbuf = curbuf; 1983 1984 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 1985 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); 1986 #ifdef FEAT_EVAL 1987 if (curbuf != was_curbuf || aborting()) 1988 #else 1989 if (curbuf != was_curbuf) 1990 #endif 1991 { 1992 // buffer changed, don't change name now 1993 retval = FAIL; 1994 goto theend; 1995 } 1996 // Exchange the file names for the current and the alternate 1997 // buffer. This makes it look like we are now editing the buffer 1998 // under the new name. Must be done before buf_write(), because 1999 // if there is no file name and 'cpo' contains 'F', it will set 2000 // the file name. 2001 fname = alt_buf->b_fname; 2002 alt_buf->b_fname = curbuf->b_fname; 2003 curbuf->b_fname = fname; 2004 fname = alt_buf->b_ffname; 2005 alt_buf->b_ffname = curbuf->b_ffname; 2006 curbuf->b_ffname = fname; 2007 fname = alt_buf->b_sfname; 2008 alt_buf->b_sfname = curbuf->b_sfname; 2009 curbuf->b_sfname = fname; 2010 buf_name_changed(curbuf); 2011 2012 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 2013 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); 2014 if (!alt_buf->b_p_bl) 2015 { 2016 alt_buf->b_p_bl = TRUE; 2017 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); 2018 } 2019 #ifdef FEAT_EVAL 2020 if (curbuf != was_curbuf || aborting()) 2021 #else 2022 if (curbuf != was_curbuf) 2023 #endif 2024 { 2025 // buffer changed, don't write the file 2026 retval = FAIL; 2027 goto theend; 2028 } 2029 2030 // If 'filetype' was empty try detecting it now. 2031 if (*curbuf->b_p_ft == NUL) 2032 { 2033 if (au_has_group((char_u *)"filetypedetect")) 2034 (void)do_doautocmd((char_u *)"filetypedetect BufRead", 2035 TRUE, NULL); 2036 do_modelines(0); 2037 } 2038 2039 // Autocommands may have changed buffer names, esp. when 2040 // 'autochdir' is set. 2041 fname = curbuf->b_sfname; 2042 } 2043 2044 name_was_missing = curbuf->b_ffname == NULL; 2045 2046 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, 2047 eap, eap->append, eap->forceit, TRUE, FALSE); 2048 2049 // After ":saveas fname" reset 'readonly'. 2050 if (eap->cmdidx == CMD_saveas) 2051 { 2052 if (retval == OK) 2053 { 2054 curbuf->b_p_ro = FALSE; 2055 redraw_tabline = TRUE; 2056 } 2057 } 2058 2059 // Change directories when the 'acd' option is set and the file name 2060 // got changed or set. 2061 if (eap->cmdidx == CMD_saveas || name_was_missing) 2062 DO_AUTOCHDIR; 2063 } 2064 2065 theend: 2066 #ifdef FEAT_BROWSE 2067 vim_free(browse_file); 2068 #endif 2069 vim_free(free_fname); 2070 return retval; 2071 } 2072 2073 /* 2074 * Check if it is allowed to overwrite a file. If b_flags has BF_NOTEDITED, 2075 * BF_NEW or BF_READERR, check for overwriting current file. 2076 * May set eap->forceit if a dialog says it's OK to overwrite. 2077 * Return OK if it's OK, FAIL if it is not. 2078 */ 2079 int 2080 check_overwrite( 2081 exarg_T *eap, 2082 buf_T *buf, 2083 char_u *fname, // file name to be used (can differ from 2084 // buf->ffname) 2085 char_u *ffname, // full path version of fname 2086 int other) // writing under other name 2087 { 2088 /* 2089 * Write to another file or b_flags set or not writing the whole file: 2090 * overwriting only allowed with '!'. 2091 */ 2092 if ( (other 2093 || (buf->b_flags & BF_NOTEDITED) 2094 || ((buf->b_flags & BF_NEW) 2095 && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) 2096 || (buf->b_flags & BF_READERR)) 2097 && !p_wa 2098 && vim_fexists(ffname)) 2099 { 2100 if (!eap->forceit && !eap->append) 2101 { 2102 #ifdef UNIX 2103 // with UNIX it is possible to open a directory 2104 if (mch_isdir(ffname)) 2105 { 2106 semsg(_(e_isadir2), ffname); 2107 return FAIL; 2108 } 2109 #endif 2110 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2111 if (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) 2112 { 2113 char_u buff[DIALOG_MSG_SIZE]; 2114 2115 dialog_msg(buff, _("Overwrite existing file \"%s\"?"), fname); 2116 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) != VIM_YES) 2117 return FAIL; 2118 eap->forceit = TRUE; 2119 } 2120 else 2121 #endif 2122 { 2123 emsg(_(e_exists)); 2124 return FAIL; 2125 } 2126 } 2127 2128 // For ":w! filename" check that no swap file exists for "filename". 2129 if (other && !emsg_silent) 2130 { 2131 char_u *dir; 2132 char_u *p; 2133 int r; 2134 char_u *swapname; 2135 2136 // We only try the first entry in 'directory', without checking if 2137 // it's writable. If the "." directory is not writable the write 2138 // will probably fail anyway. 2139 // Use 'shortname' of the current buffer, since there is no buffer 2140 // for the written file. 2141 if (*p_dir == NUL) 2142 { 2143 dir = alloc(5); 2144 if (dir == NULL) 2145 return FAIL; 2146 STRCPY(dir, "."); 2147 } 2148 else 2149 { 2150 dir = alloc(MAXPATHL); 2151 if (dir == NULL) 2152 return FAIL; 2153 p = p_dir; 2154 copy_option_part(&p, dir, MAXPATHL, ","); 2155 } 2156 swapname = makeswapname(fname, ffname, curbuf, dir); 2157 vim_free(dir); 2158 r = vim_fexists(swapname); 2159 if (r) 2160 { 2161 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2162 if (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) 2163 { 2164 char_u buff[DIALOG_MSG_SIZE]; 2165 2166 dialog_msg(buff, 2167 _("Swap file \"%s\" exists, overwrite anyway?"), 2168 swapname); 2169 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) 2170 != VIM_YES) 2171 { 2172 vim_free(swapname); 2173 return FAIL; 2174 } 2175 eap->forceit = TRUE; 2176 } 2177 else 2178 #endif 2179 { 2180 semsg(_("E768: Swap file exists: %s (:silent! overrides)"), 2181 swapname); 2182 vim_free(swapname); 2183 return FAIL; 2184 } 2185 } 2186 vim_free(swapname); 2187 } 2188 } 2189 return OK; 2190 } 2191 2192 /* 2193 * Handle ":wnext", ":wNext" and ":wprevious" commands. 2194 */ 2195 void 2196 ex_wnext(exarg_T *eap) 2197 { 2198 int i; 2199 2200 if (eap->cmd[1] == 'n') 2201 i = curwin->w_arg_idx + (int)eap->line2; 2202 else 2203 i = curwin->w_arg_idx - (int)eap->line2; 2204 eap->line1 = 1; 2205 eap->line2 = curbuf->b_ml.ml_line_count; 2206 if (do_write(eap) != FAIL) 2207 do_argfile(eap, i); 2208 } 2209 2210 /* 2211 * ":wall", ":wqall" and ":xall": Write all changed files (and exit). 2212 */ 2213 void 2214 do_wqall(exarg_T *eap) 2215 { 2216 buf_T *buf; 2217 int error = 0; 2218 int save_forceit = eap->forceit; 2219 2220 if (eap->cmdidx == CMD_xall || eap->cmdidx == CMD_wqall) 2221 exiting = TRUE; 2222 2223 FOR_ALL_BUFFERS(buf) 2224 { 2225 #ifdef FEAT_TERMINAL 2226 if (exiting && term_job_running(buf->b_term)) 2227 { 2228 no_write_message_nobang(buf); 2229 ++error; 2230 } 2231 else 2232 #endif 2233 if (bufIsChanged(buf) && !bt_dontwrite(buf)) 2234 { 2235 /* 2236 * Check if there is a reason the buffer cannot be written: 2237 * 1. if the 'write' option is set 2238 * 2. if there is no file name (even after browsing) 2239 * 3. if the 'readonly' is set (even after a dialog) 2240 * 4. if overwriting is allowed (even after a dialog) 2241 */ 2242 if (not_writing()) 2243 { 2244 ++error; 2245 break; 2246 } 2247 #ifdef FEAT_BROWSE 2248 // ":browse wall": ask for file name if there isn't one 2249 if (buf->b_ffname == NULL && (cmdmod.cmod_flags & CMOD_BROWSE)) 2250 browse_save_fname(buf); 2251 #endif 2252 if (buf->b_ffname == NULL) 2253 { 2254 semsg(_("E141: No file name for buffer %ld"), 2255 (long)buf->b_fnum); 2256 ++error; 2257 } 2258 else if (check_readonly(&eap->forceit, buf) 2259 || check_overwrite(eap, buf, buf->b_fname, buf->b_ffname, 2260 FALSE) == FAIL) 2261 { 2262 ++error; 2263 } 2264 else 2265 { 2266 bufref_T bufref; 2267 2268 set_bufref(&bufref, buf); 2269 if (buf_write_all(buf, eap->forceit) == FAIL) 2270 ++error; 2271 // an autocommand may have deleted the buffer 2272 if (!bufref_valid(&bufref)) 2273 buf = firstbuf; 2274 } 2275 eap->forceit = save_forceit; // check_overwrite() may set it 2276 } 2277 } 2278 if (exiting) 2279 { 2280 if (!error) 2281 getout(0); // exit Vim 2282 not_exiting(); 2283 } 2284 } 2285 2286 /* 2287 * Check the 'write' option. 2288 * Return TRUE and give a message when it's not set. 2289 */ 2290 static int 2291 not_writing(void) 2292 { 2293 if (p_write) 2294 return FALSE; 2295 emsg(_("E142: File not written: Writing is disabled by 'write' option")); 2296 return TRUE; 2297 } 2298 2299 /* 2300 * Check if a buffer is read-only (either 'readonly' option is set or file is 2301 * read-only). Ask for overruling in a dialog. Return TRUE and give an error 2302 * message when the buffer is readonly. 2303 */ 2304 static int 2305 check_readonly(int *forceit, buf_T *buf) 2306 { 2307 stat_T st; 2308 2309 // Handle a file being readonly when the 'readonly' option is set or when 2310 // the file exists and permissions are read-only. 2311 // We will send 0777 to check_file_readonly(), as the "perm" variable is 2312 // important for device checks but not here. 2313 if (!*forceit && (buf->b_p_ro 2314 || (mch_stat((char *)buf->b_ffname, &st) >= 0 2315 && check_file_readonly(buf->b_ffname, 0777)))) 2316 { 2317 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2318 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) 2319 && buf->b_fname != NULL) 2320 { 2321 char_u buff[DIALOG_MSG_SIZE]; 2322 2323 if (buf->b_p_ro) 2324 dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"), 2325 buf->b_fname); 2326 else 2327 dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"), 2328 buf->b_fname); 2329 2330 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES) 2331 { 2332 // Set forceit, to force the writing of a readonly file 2333 *forceit = TRUE; 2334 return FALSE; 2335 } 2336 else 2337 return TRUE; 2338 } 2339 else 2340 #endif 2341 if (buf->b_p_ro) 2342 emsg(_(e_readonly)); 2343 else 2344 semsg(_("E505: \"%s\" is read-only (add ! to override)"), 2345 buf->b_fname); 2346 return TRUE; 2347 } 2348 2349 return FALSE; 2350 } 2351 2352 /* 2353 * Try to abandon the current file and edit a new or existing file. 2354 * "fnum" is the number of the file, if zero use "ffname_arg"/"sfname_arg". 2355 * "lnum" is the line number for the cursor in the new file (if non-zero). 2356 * 2357 * Return: 2358 * GETFILE_ERROR for "normal" error, 2359 * GETFILE_NOT_WRITTEN for "not written" error, 2360 * GETFILE_SAME_FILE for success 2361 * GETFILE_OPEN_OTHER for successfully opening another file. 2362 */ 2363 int 2364 getfile( 2365 int fnum, 2366 char_u *ffname_arg, 2367 char_u *sfname_arg, 2368 int setpm, 2369 linenr_T lnum, 2370 int forceit) 2371 { 2372 char_u *ffname = ffname_arg; 2373 char_u *sfname = sfname_arg; 2374 int other; 2375 int retval; 2376 char_u *free_me = NULL; 2377 2378 if (text_locked()) 2379 return GETFILE_ERROR; 2380 if (curbuf_locked()) 2381 return GETFILE_ERROR; 2382 2383 if (fnum == 0) 2384 { 2385 // make ffname full path, set sfname 2386 fname_expand(curbuf, &ffname, &sfname); 2387 other = otherfile(ffname); 2388 free_me = ffname; // has been allocated, free() later 2389 } 2390 else 2391 other = (fnum != curbuf->b_fnum); 2392 2393 if (other) 2394 ++no_wait_return; // don't wait for autowrite message 2395 if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf) 2396 && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) 2397 { 2398 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2399 if (p_confirm && p_write) 2400 dialog_changed(curbuf, FALSE); 2401 if (curbufIsChanged()) 2402 #endif 2403 { 2404 if (other) 2405 --no_wait_return; 2406 no_write_message(); 2407 retval = GETFILE_NOT_WRITTEN; // file has been changed 2408 goto theend; 2409 } 2410 } 2411 if (other) 2412 --no_wait_return; 2413 if (setpm) 2414 setpcmark(); 2415 if (!other) 2416 { 2417 if (lnum != 0) 2418 curwin->w_cursor.lnum = lnum; 2419 check_cursor_lnum(); 2420 beginline(BL_SOL | BL_FIX); 2421 retval = GETFILE_SAME_FILE; // it's in the same file 2422 } 2423 else if (do_ecmd(fnum, ffname, sfname, NULL, lnum, 2424 (buf_hide(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0), 2425 curwin) == OK) 2426 retval = GETFILE_OPEN_OTHER; // opened another file 2427 else 2428 retval = GETFILE_ERROR; // error encountered 2429 2430 theend: 2431 vim_free(free_me); 2432 return retval; 2433 } 2434 2435 /* 2436 * start editing a new file 2437 * 2438 * fnum: file number; if zero use ffname/sfname 2439 * ffname: the file name 2440 * - full path if sfname used, 2441 * - any file name if sfname is NULL 2442 * - empty string to re-edit with the same file name (but may be 2443 * in a different directory) 2444 * - NULL to start an empty buffer 2445 * sfname: the short file name (or NULL) 2446 * eap: contains the command to be executed after loading the file and 2447 * forced 'ff' and 'fenc' 2448 * newlnum: if > 0: put cursor on this line number (if possible) 2449 * if ECMD_LASTL: use last position in loaded file 2450 * if ECMD_LAST: use last position in all files 2451 * if ECMD_ONE: use first line 2452 * flags: 2453 * ECMD_HIDE: if TRUE don't free the current buffer 2454 * ECMD_SET_HELP: set b_help flag of (new) buffer before opening file 2455 * ECMD_OLDBUF: use existing buffer if it exists 2456 * ECMD_FORCEIT: ! used for Ex command 2457 * ECMD_ADDBUF: don't edit, just add to buffer list 2458 * ECMD_ALTBUF: like ECMD_ADDBUF and also set the alternate file 2459 * oldwin: Should be "curwin" when editing a new buffer in the current 2460 * window, NULL when splitting the window first. When not NULL info 2461 * of the previous buffer for "oldwin" is stored. 2462 * 2463 * return FAIL for failure, OK otherwise 2464 */ 2465 int 2466 do_ecmd( 2467 int fnum, 2468 char_u *ffname, 2469 char_u *sfname, 2470 exarg_T *eap, // can be NULL! 2471 linenr_T newlnum, 2472 int flags, 2473 win_T *oldwin) 2474 { 2475 int other_file; // TRUE if editing another file 2476 int oldbuf; // TRUE if using existing buffer 2477 int auto_buf = FALSE; // TRUE if autocommands brought us 2478 // into the buffer unexpectedly 2479 char_u *new_name = NULL; 2480 #if defined(FEAT_EVAL) 2481 int did_set_swapcommand = FALSE; 2482 #endif 2483 buf_T *buf; 2484 bufref_T bufref; 2485 bufref_T old_curbuf; 2486 char_u *free_fname = NULL; 2487 #ifdef FEAT_BROWSE 2488 char_u dot_path[] = "."; 2489 char_u *browse_file = NULL; 2490 #endif 2491 int retval = FAIL; 2492 long n; 2493 pos_T orig_pos; 2494 linenr_T topline = 0; 2495 int newcol = -1; 2496 int solcol = -1; 2497 pos_T *pos; 2498 char_u *command = NULL; 2499 #ifdef FEAT_SPELL 2500 int did_get_winopts = FALSE; 2501 #endif 2502 int readfile_flags = 0; 2503 int did_inc_redrawing_disabled = FALSE; 2504 long *so_ptr = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so; 2505 2506 #ifdef FEAT_PROP_POPUP 2507 if (ERROR_IF_TERM_POPUP_WINDOW) 2508 return FAIL; 2509 #endif 2510 2511 if (eap != NULL) 2512 command = eap->do_ecmd_cmd; 2513 set_bufref(&old_curbuf, curbuf); 2514 2515 if (fnum != 0) 2516 { 2517 if (fnum == curbuf->b_fnum) // file is already being edited 2518 return OK; // nothing to do 2519 other_file = TRUE; 2520 } 2521 else 2522 { 2523 #ifdef FEAT_BROWSE 2524 if ((cmdmod.cmod_flags & CMOD_BROWSE) && !exiting) 2525 { 2526 if ( 2527 # ifdef FEAT_GUI 2528 !gui.in_use && 2529 # endif 2530 au_has_group((char_u *)"FileExplorer")) 2531 { 2532 // No browsing supported but we do have the file explorer: 2533 // Edit the directory. 2534 if (ffname == NULL || !mch_isdir(ffname)) 2535 ffname = dot_path; 2536 } 2537 else 2538 { 2539 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, 2540 NULL, NULL, NULL, curbuf); 2541 if (browse_file == NULL) 2542 goto theend; 2543 ffname = browse_file; 2544 } 2545 } 2546 #endif 2547 // if no short name given, use ffname for short name 2548 if (sfname == NULL) 2549 sfname = ffname; 2550 #ifdef USE_FNAME_CASE 2551 if (sfname != NULL) 2552 fname_case(sfname, 0); // set correct case for sfname 2553 #endif 2554 2555 if ((flags & (ECMD_ADDBUF | ECMD_ALTBUF)) 2556 && (ffname == NULL || *ffname == NUL)) 2557 goto theend; 2558 2559 if (ffname == NULL) 2560 other_file = TRUE; 2561 // there is no file name 2562 else if (*ffname == NUL && curbuf->b_ffname == NULL) 2563 other_file = FALSE; 2564 else 2565 { 2566 if (*ffname == NUL) // re-edit with same file name 2567 { 2568 ffname = curbuf->b_ffname; 2569 sfname = curbuf->b_fname; 2570 } 2571 free_fname = fix_fname(ffname); // may expand to full path name 2572 if (free_fname != NULL) 2573 ffname = free_fname; 2574 other_file = otherfile(ffname); 2575 } 2576 } 2577 2578 /* 2579 * If the file was changed we may not be allowed to abandon it: 2580 * - if we are going to re-edit the same file 2581 * - or if we are the only window on this file and if ECMD_HIDE is FALSE 2582 */ 2583 if ( ((!other_file && !(flags & ECMD_OLDBUF)) 2584 || (curbuf->b_nwindows == 1 2585 && !(flags & (ECMD_HIDE | ECMD_ADDBUF | ECMD_ALTBUF)))) 2586 && check_changed(curbuf, (p_awa ? CCGD_AW : 0) 2587 | (other_file ? 0 : CCGD_MULTWIN) 2588 | ((flags & ECMD_FORCEIT) ? CCGD_FORCEIT : 0) 2589 | (eap == NULL ? 0 : CCGD_EXCMD))) 2590 { 2591 if (fnum == 0 && other_file && ffname != NULL) 2592 (void)setaltfname(ffname, sfname, newlnum < 0 ? 0 : newlnum); 2593 goto theend; 2594 } 2595 2596 /* 2597 * End Visual mode before switching to another buffer, so the text can be 2598 * copied into the GUI selection buffer. 2599 */ 2600 reset_VIsual(); 2601 2602 #if defined(FEAT_EVAL) 2603 if ((command != NULL || newlnum > (linenr_T)0) 2604 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) 2605 { 2606 int len; 2607 char_u *p; 2608 2609 // Set v:swapcommand for the SwapExists autocommands. 2610 if (command != NULL) 2611 len = (int)STRLEN(command) + 3; 2612 else 2613 len = 30; 2614 p = alloc(len); 2615 if (p != NULL) 2616 { 2617 if (command != NULL) 2618 vim_snprintf((char *)p, len, ":%s\r", command); 2619 else 2620 vim_snprintf((char *)p, len, "%ldG", (long)newlnum); 2621 set_vim_var_string(VV_SWAPCOMMAND, p, -1); 2622 did_set_swapcommand = TRUE; 2623 vim_free(p); 2624 } 2625 } 2626 #endif 2627 2628 /* 2629 * If we are starting to edit another file, open a (new) buffer. 2630 * Otherwise we re-use the current buffer. 2631 */ 2632 if (other_file) 2633 { 2634 if (!(flags & (ECMD_ADDBUF | ECMD_ALTBUF))) 2635 { 2636 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0) 2637 curwin->w_alt_fnum = curbuf->b_fnum; 2638 if (oldwin != NULL) 2639 buflist_altfpos(oldwin); 2640 } 2641 2642 if (fnum) 2643 buf = buflist_findnr(fnum); 2644 else 2645 { 2646 if (flags & (ECMD_ADDBUF | ECMD_ALTBUF)) 2647 { 2648 // Default the line number to zero to avoid that a wininfo item 2649 // is added for the current window. 2650 linenr_T tlnum = 0; 2651 buf_T *newbuf; 2652 2653 if (command != NULL) 2654 { 2655 tlnum = atol((char *)command); 2656 if (tlnum <= 0) 2657 tlnum = 1L; 2658 } 2659 // Add BLN_NOCURWIN to avoid a new wininfo items are associated 2660 // with the current window. 2661 newbuf = buflist_new(ffname, sfname, tlnum, 2662 BLN_LISTED | BLN_NOCURWIN); 2663 if (newbuf != NULL && (flags & ECMD_ALTBUF)) 2664 curwin->w_alt_fnum = newbuf->b_fnum; 2665 goto theend; 2666 } 2667 buf = buflist_new(ffname, sfname, 0L, 2668 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); 2669 2670 // autocommands may change curwin and curbuf 2671 if (oldwin != NULL) 2672 oldwin = curwin; 2673 set_bufref(&old_curbuf, curbuf); 2674 } 2675 if (buf == NULL) 2676 goto theend; 2677 if (buf->b_ml.ml_mfp == NULL) // no memfile yet 2678 { 2679 oldbuf = FALSE; 2680 } 2681 else // existing memfile 2682 { 2683 oldbuf = TRUE; 2684 set_bufref(&bufref, buf); 2685 (void)buf_check_timestamp(buf, FALSE); 2686 // Check if autocommands made the buffer invalid or changed the 2687 // current buffer. 2688 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf) 2689 goto theend; 2690 #ifdef FEAT_EVAL 2691 if (aborting()) // autocmds may abort script processing 2692 goto theend; 2693 #endif 2694 } 2695 2696 // May jump to last used line number for a loaded buffer or when asked 2697 // for explicitly 2698 if ((oldbuf && newlnum == ECMD_LASTL) || newlnum == ECMD_LAST) 2699 { 2700 pos = buflist_findfpos(buf); 2701 newlnum = pos->lnum; 2702 solcol = pos->col; 2703 } 2704 2705 /* 2706 * Make the (new) buffer the one used by the current window. 2707 * If the old buffer becomes unused, free it if ECMD_HIDE is FALSE. 2708 * If the current buffer was empty and has no file name, curbuf 2709 * is returned by buflist_new(), nothing to do here. 2710 */ 2711 if (buf != curbuf) 2712 { 2713 bufref_T save_au_new_curbuf; 2714 #ifdef FEAT_CMDWIN 2715 int save_cmdwin_type = cmdwin_type; 2716 2717 // BufLeave applies to the old buffer. 2718 cmdwin_type = 0; 2719 #endif 2720 /* 2721 * Be careful: The autocommands may delete any buffer and change 2722 * the current buffer. 2723 * - If the buffer we are going to edit is deleted, give up. 2724 * - If the current buffer is deleted, prefer to load the new 2725 * buffer when loading a buffer is required. This avoids 2726 * loading another buffer which then must be closed again. 2727 * - If we ended up in the new buffer already, need to skip a few 2728 * things, set auto_buf. 2729 */ 2730 if (buf->b_fname != NULL) 2731 new_name = vim_strsave(buf->b_fname); 2732 save_au_new_curbuf = au_new_curbuf; 2733 set_bufref(&au_new_curbuf, buf); 2734 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 2735 #ifdef FEAT_CMDWIN 2736 cmdwin_type = save_cmdwin_type; 2737 #endif 2738 if (!bufref_valid(&au_new_curbuf)) 2739 { 2740 // new buffer has been deleted 2741 delbuf_msg(new_name); // frees new_name 2742 au_new_curbuf = save_au_new_curbuf; 2743 goto theend; 2744 } 2745 #ifdef FEAT_EVAL 2746 if (aborting()) // autocmds may abort script processing 2747 { 2748 vim_free(new_name); 2749 au_new_curbuf = save_au_new_curbuf; 2750 goto theend; 2751 } 2752 #endif 2753 if (buf == curbuf) // already in new buffer 2754 auto_buf = TRUE; 2755 else 2756 { 2757 win_T *the_curwin = curwin; 2758 int did_decrement; 2759 buf_T *was_curbuf = curbuf; 2760 2761 // Set the w_closing flag to avoid that autocommands close the 2762 // window. And set b_locked for the same reason. 2763 the_curwin->w_closing = TRUE; 2764 ++buf->b_locked; 2765 2766 if (curbuf == old_curbuf.br_buf) 2767 buf_copy_options(buf, BCO_ENTER); 2768 2769 // Close the link to the current buffer. This will set 2770 // oldwin->w_buffer to NULL. 2771 u_sync(FALSE); 2772 did_decrement = close_buffer(oldwin, curbuf, 2773 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE, FALSE); 2774 2775 // Autocommands may have closed the window. 2776 if (win_valid(the_curwin)) 2777 the_curwin->w_closing = FALSE; 2778 --buf->b_locked; 2779 2780 #ifdef FEAT_EVAL 2781 // autocmds may abort script processing 2782 if (aborting() && curwin->w_buffer != NULL) 2783 { 2784 vim_free(new_name); 2785 au_new_curbuf = save_au_new_curbuf; 2786 goto theend; 2787 } 2788 #endif 2789 // Be careful again, like above. 2790 if (!bufref_valid(&au_new_curbuf)) 2791 { 2792 // new buffer has been deleted 2793 delbuf_msg(new_name); // frees new_name 2794 au_new_curbuf = save_au_new_curbuf; 2795 goto theend; 2796 } 2797 if (buf == curbuf) // already in new buffer 2798 { 2799 // close_buffer() has decremented the window count, 2800 // increment it again here and restore w_buffer. 2801 if (did_decrement && buf_valid(was_curbuf)) 2802 ++was_curbuf->b_nwindows; 2803 if (win_valid_any_tab(oldwin) && oldwin->w_buffer == NULL) 2804 oldwin->w_buffer = was_curbuf; 2805 auto_buf = TRUE; 2806 } 2807 else 2808 { 2809 #ifdef FEAT_SYN_HL 2810 /* 2811 * <VN> We could instead free the synblock 2812 * and re-attach to buffer, perhaps. 2813 */ 2814 if (curwin->w_buffer == NULL 2815 || curwin->w_s == &(curwin->w_buffer->b_s)) 2816 curwin->w_s = &(buf->b_s); 2817 #endif 2818 curwin->w_buffer = buf; 2819 curbuf = buf; 2820 ++curbuf->b_nwindows; 2821 2822 // Set 'fileformat', 'binary' and 'fenc' when forced. 2823 if (!oldbuf && eap != NULL) 2824 { 2825 set_file_options(TRUE, eap); 2826 set_forced_fenc(eap); 2827 } 2828 } 2829 2830 // May get the window options from the last time this buffer 2831 // was in this window (or another window). If not used 2832 // before, reset the local window options to the global 2833 // values. Also restores old folding stuff. 2834 get_winopts(curbuf); 2835 #ifdef FEAT_SPELL 2836 did_get_winopts = TRUE; 2837 #endif 2838 } 2839 vim_free(new_name); 2840 au_new_curbuf = save_au_new_curbuf; 2841 } 2842 2843 curwin->w_pcmark.lnum = 1; 2844 curwin->w_pcmark.col = 0; 2845 } 2846 else // !other_file 2847 { 2848 if ((flags & (ECMD_ADDBUF | ECMD_ALTBUF)) || check_fname() == FAIL) 2849 goto theend; 2850 2851 oldbuf = (flags & ECMD_OLDBUF); 2852 } 2853 2854 // Don't redraw until the cursor is in the right line, otherwise 2855 // autocommands may cause ml_get errors. 2856 ++RedrawingDisabled; 2857 did_inc_redrawing_disabled = TRUE; 2858 2859 buf = curbuf; 2860 if ((flags & ECMD_SET_HELP) || keep_help_flag) 2861 { 2862 prepare_help_buffer(); 2863 } 2864 else 2865 { 2866 // Don't make a buffer listed if it's a help buffer. Useful when 2867 // using CTRL-O to go back to a help file. 2868 if (!curbuf->b_help) 2869 set_buflisted(TRUE); 2870 } 2871 2872 // If autocommands change buffers under our fingers, forget about 2873 // editing the file. 2874 if (buf != curbuf) 2875 goto theend; 2876 #ifdef FEAT_EVAL 2877 if (aborting()) // autocmds may abort script processing 2878 goto theend; 2879 #endif 2880 2881 // Since we are starting to edit a file, consider the filetype to be 2882 // unset. Helps for when an autocommand changes files and expects syntax 2883 // highlighting to work in the other file. 2884 did_filetype = FALSE; 2885 2886 /* 2887 * other_file oldbuf 2888 * FALSE FALSE re-edit same file, buffer is re-used 2889 * FALSE TRUE re-edit same file, nothing changes 2890 * TRUE FALSE start editing new file, new buffer 2891 * TRUE TRUE start editing in existing buffer (nothing to do) 2892 */ 2893 if (!other_file && !oldbuf) // re-use the buffer 2894 { 2895 set_last_cursor(curwin); // may set b_last_cursor 2896 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) 2897 { 2898 newlnum = curwin->w_cursor.lnum; 2899 solcol = curwin->w_cursor.col; 2900 } 2901 buf = curbuf; 2902 if (buf->b_fname != NULL) 2903 new_name = vim_strsave(buf->b_fname); 2904 else 2905 new_name = NULL; 2906 set_bufref(&bufref, buf); 2907 2908 // If the buffer was used before, store the current contents so that 2909 // the reload can be undone. Do not do this if the (empty) buffer is 2910 // being re-used for another file. 2911 if (!(curbuf->b_flags & BF_NEVERLOADED) 2912 && (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)) 2913 { 2914 // Sync first so that this is a separate undo-able action. 2915 u_sync(FALSE); 2916 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) 2917 == FAIL) 2918 { 2919 vim_free(new_name); 2920 goto theend; 2921 } 2922 u_unchanged(curbuf); 2923 buf_freeall(curbuf, BFA_KEEP_UNDO); 2924 2925 // tell readfile() not to clear or reload undo info 2926 readfile_flags = READ_KEEP_UNDO; 2927 } 2928 else 2929 buf_freeall(curbuf, 0); // free all things for buffer 2930 2931 // If autocommands deleted the buffer we were going to re-edit, give 2932 // up and jump to the end. 2933 if (!bufref_valid(&bufref)) 2934 { 2935 delbuf_msg(new_name); // frees new_name 2936 goto theend; 2937 } 2938 vim_free(new_name); 2939 2940 // If autocommands change buffers under our fingers, forget about 2941 // re-editing the file. Should do the buf_clear_file(), but perhaps 2942 // the autocommands changed the buffer... 2943 if (buf != curbuf) 2944 goto theend; 2945 #ifdef FEAT_EVAL 2946 if (aborting()) // autocmds may abort script processing 2947 goto theend; 2948 #endif 2949 buf_clear_file(curbuf); 2950 curbuf->b_op_start.lnum = 0; // clear '[ and '] marks 2951 curbuf->b_op_end.lnum = 0; 2952 } 2953 2954 /* 2955 * If we get here we are sure to start editing 2956 */ 2957 // Assume success now 2958 retval = OK; 2959 2960 /* 2961 * Check if we are editing the w_arg_idx file in the argument list. 2962 */ 2963 check_arg_idx(curwin); 2964 2965 if (!auto_buf) 2966 { 2967 /* 2968 * Set cursor and init window before reading the file and executing 2969 * autocommands. This allows for the autocommands to position the 2970 * cursor. 2971 */ 2972 curwin_init(); 2973 2974 #ifdef FEAT_FOLDING 2975 // It's possible that all lines in the buffer changed. Need to update 2976 // automatic folding for all windows where it's used. 2977 { 2978 win_T *win; 2979 tabpage_T *tp; 2980 2981 FOR_ALL_TAB_WINDOWS(tp, win) 2982 if (win->w_buffer == curbuf) 2983 foldUpdateAll(win); 2984 } 2985 #endif 2986 2987 // Change directories when the 'acd' option is set. 2988 DO_AUTOCHDIR; 2989 2990 /* 2991 * Careful: open_buffer() and apply_autocmds() may change the current 2992 * buffer and window. 2993 */ 2994 orig_pos = curwin->w_cursor; 2995 topline = curwin->w_topline; 2996 if (!oldbuf) // need to read the file 2997 { 2998 #ifdef FEAT_PROP_POPUP 2999 // Don't use the swap-exists dialog for a popup window, can't edit 3000 // the buffer. 3001 if (WIN_IS_POPUP(curwin)) 3002 curbuf->b_flags |= BF_NO_SEA; 3003 #endif 3004 swap_exists_action = SEA_DIALOG; 3005 curbuf->b_flags |= BF_CHECK_RO; // set/reset 'ro' flag 3006 3007 /* 3008 * Open the buffer and read the file. 3009 */ 3010 #if defined(FEAT_EVAL) 3011 if (should_abort(open_buffer(FALSE, eap, readfile_flags))) 3012 retval = FAIL; 3013 #else 3014 (void)open_buffer(FALSE, eap, readfile_flags); 3015 #endif 3016 3017 #ifdef FEAT_PROP_POPUP 3018 curbuf->b_flags &= ~BF_NO_SEA; 3019 #endif 3020 if (swap_exists_action == SEA_QUIT) 3021 retval = FAIL; 3022 handle_swap_exists(&old_curbuf); 3023 } 3024 else 3025 { 3026 // Read the modelines, but only to set window-local options. Any 3027 // buffer-local options have already been set and may have been 3028 // changed by the user. 3029 do_modelines(OPT_WINONLY); 3030 3031 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, 3032 &retval); 3033 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, 3034 &retval); 3035 } 3036 check_arg_idx(curwin); 3037 3038 // If autocommands change the cursor position or topline, we should 3039 // keep it. Also when it moves within a line. But not when it moves 3040 // to the first non-blank. 3041 if (!EQUAL_POS(curwin->w_cursor, orig_pos)) 3042 { 3043 char_u *text = ml_get_curline(); 3044 3045 if (curwin->w_cursor.lnum != orig_pos.lnum 3046 || curwin->w_cursor.col != (int)(skipwhite(text) - text)) 3047 { 3048 newlnum = curwin->w_cursor.lnum; 3049 newcol = curwin->w_cursor.col; 3050 } 3051 } 3052 if (curwin->w_topline == topline) 3053 topline = 0; 3054 3055 // Even when cursor didn't move we need to recompute topline. 3056 changed_line_abv_curs(); 3057 3058 #ifdef FEAT_TITLE 3059 maketitle(); 3060 #endif 3061 #if defined(FEAT_PROP_POPUP) && defined(FEAT_QUICKFIX) 3062 if (WIN_IS_POPUP(curwin) && curwin->w_p_pvw && retval != FAIL) 3063 popup_set_title(curwin); 3064 #endif 3065 } 3066 3067 #ifdef FEAT_DIFF 3068 // Tell the diff stuff that this buffer is new and/or needs updating. 3069 // Also needed when re-editing the same buffer, because unloading will 3070 // have removed it as a diff buffer. 3071 if (curwin->w_p_diff) 3072 { 3073 diff_buf_add(curbuf); 3074 diff_invalidate(curbuf); 3075 } 3076 #endif 3077 3078 #ifdef FEAT_SPELL 3079 // If the window options were changed may need to set the spell language. 3080 // Can only do this after the buffer has been properly setup. 3081 if (did_get_winopts && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) 3082 (void)did_set_spelllang(curwin); 3083 #endif 3084 3085 if (command == NULL) 3086 { 3087 if (newcol >= 0) // position set by autocommands 3088 { 3089 curwin->w_cursor.lnum = newlnum; 3090 curwin->w_cursor.col = newcol; 3091 check_cursor(); 3092 } 3093 else if (newlnum > 0) // line number from caller or old position 3094 { 3095 curwin->w_cursor.lnum = newlnum; 3096 check_cursor_lnum(); 3097 if (solcol >= 0 && !p_sol) 3098 { 3099 // 'sol' is off: Use last known column. 3100 curwin->w_cursor.col = solcol; 3101 check_cursor_col(); 3102 curwin->w_cursor.coladd = 0; 3103 curwin->w_set_curswant = TRUE; 3104 } 3105 else 3106 beginline(BL_SOL | BL_FIX); 3107 } 3108 else // no line number, go to last line in Ex mode 3109 { 3110 if (exmode_active) 3111 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 3112 beginline(BL_WHITE | BL_FIX); 3113 } 3114 } 3115 3116 // Check if cursors in other windows on the same buffer are still valid 3117 check_lnums(FALSE); 3118 3119 /* 3120 * Did not read the file, need to show some info about the file. 3121 * Do this after setting the cursor. 3122 */ 3123 if (oldbuf && !auto_buf) 3124 { 3125 int msg_scroll_save = msg_scroll; 3126 3127 // Obey the 'O' flag in 'cpoptions': overwrite any previous file 3128 // message. 3129 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) 3130 msg_scroll = FALSE; 3131 if (!msg_scroll) // wait a bit when overwriting an error msg 3132 check_for_delay(FALSE); 3133 msg_start(); 3134 msg_scroll = msg_scroll_save; 3135 msg_scrolled_ign = TRUE; 3136 3137 if (!shortmess(SHM_FILEINFO)) 3138 fileinfo(FALSE, TRUE, FALSE); 3139 3140 msg_scrolled_ign = FALSE; 3141 } 3142 3143 #ifdef FEAT_VIMINFO 3144 curbuf->b_last_used = vim_time(); 3145 #endif 3146 3147 if (command != NULL) 3148 do_cmdline(command, NULL, NULL, DOCMD_VERBOSE|DOCMD_RANGEOK); 3149 3150 #ifdef FEAT_KEYMAP 3151 if (curbuf->b_kmap_state & KEYMAP_INIT) 3152 (void)keymap_init(); 3153 #endif 3154 3155 --RedrawingDisabled; 3156 did_inc_redrawing_disabled = FALSE; 3157 if (!skip_redraw) 3158 { 3159 n = *so_ptr; 3160 if (topline == 0 && command == NULL) 3161 *so_ptr = 9999; // force cursor halfway the window 3162 update_topline(); 3163 curwin->w_scbind_pos = curwin->w_topline; 3164 *so_ptr = n; 3165 redraw_curbuf_later(NOT_VALID); // redraw this buffer later 3166 } 3167 3168 if (p_im) 3169 need_start_insertmode = TRUE; 3170 3171 #ifdef FEAT_AUTOCHDIR 3172 // Change directories when the 'acd' option is set and we aren't already in 3173 // that directory (should already be done above). Expect getcwd() to be 3174 // faster than calling shorten_fnames() unnecessarily. 3175 if (p_acd && curbuf->b_ffname != NULL) 3176 { 3177 char_u curdir[MAXPATHL]; 3178 char_u filedir[MAXPATHL]; 3179 3180 vim_strncpy(filedir, curbuf->b_ffname, MAXPATHL - 1); 3181 *gettail_sep(filedir) = NUL; 3182 if (mch_dirname(curdir, MAXPATHL) != FAIL 3183 && vim_fnamecmp(curdir, filedir) != 0) 3184 do_autochdir(); 3185 } 3186 #endif 3187 3188 #if defined(FEAT_NETBEANS_INTG) 3189 if (curbuf->b_ffname != NULL) 3190 { 3191 # ifdef FEAT_NETBEANS_INTG 3192 if ((flags & ECMD_SET_HELP) != ECMD_SET_HELP) 3193 netbeans_file_opened(curbuf); 3194 # endif 3195 } 3196 #endif 3197 3198 theend: 3199 if (did_inc_redrawing_disabled) 3200 --RedrawingDisabled; 3201 #if defined(FEAT_EVAL) 3202 if (did_set_swapcommand) 3203 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); 3204 #endif 3205 #ifdef FEAT_BROWSE 3206 vim_free(browse_file); 3207 #endif 3208 vim_free(free_fname); 3209 return retval; 3210 } 3211 3212 static void 3213 delbuf_msg(char_u *name) 3214 { 3215 semsg(_("E143: Autocommands unexpectedly deleted new buffer %s"), 3216 name == NULL ? (char_u *)"" : name); 3217 vim_free(name); 3218 au_new_curbuf.br_buf = NULL; 3219 au_new_curbuf.br_buf_free_count = 0; 3220 } 3221 3222 static int append_indent = 0; // autoindent for first line 3223 3224 /* 3225 * ":insert" and ":append", also used by ":change" 3226 */ 3227 void 3228 ex_append(exarg_T *eap) 3229 { 3230 char_u *theline; 3231 int did_undo = FALSE; 3232 linenr_T lnum = eap->line2; 3233 int indent = 0; 3234 char_u *p; 3235 int vcol; 3236 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); 3237 3238 #ifdef FEAT_EVAL 3239 if (not_in_vim9(eap) == FAIL) 3240 return; 3241 #endif 3242 // the ! flag toggles autoindent 3243 if (eap->forceit) 3244 curbuf->b_p_ai = !curbuf->b_p_ai; 3245 3246 // First autoindent comes from the line we start on 3247 if (eap->cmdidx != CMD_change && curbuf->b_p_ai && lnum > 0) 3248 append_indent = get_indent_lnum(lnum); 3249 3250 if (eap->cmdidx != CMD_append) 3251 --lnum; 3252 3253 // when the buffer is empty need to delete the dummy line 3254 if (empty && lnum == 1) 3255 lnum = 0; 3256 3257 State = INSERT; // behave like in Insert mode 3258 if (curbuf->b_p_iminsert == B_IMODE_LMAP) 3259 State |= LANGMAP; 3260 3261 for (;;) 3262 { 3263 msg_scroll = TRUE; 3264 need_wait_return = FALSE; 3265 if (curbuf->b_p_ai) 3266 { 3267 if (append_indent >= 0) 3268 { 3269 indent = append_indent; 3270 append_indent = -1; 3271 } 3272 else if (lnum > 0) 3273 indent = get_indent_lnum(lnum); 3274 } 3275 ex_keep_indent = FALSE; 3276 if (eap->getline == NULL) 3277 { 3278 // No getline() function, use the lines that follow. This ends 3279 // when there is no more. 3280 if (eap->nextcmd == NULL || *eap->nextcmd == NUL) 3281 break; 3282 p = vim_strchr(eap->nextcmd, NL); 3283 if (p == NULL) 3284 p = eap->nextcmd + STRLEN(eap->nextcmd); 3285 theline = vim_strnsave(eap->nextcmd, p - eap->nextcmd); 3286 if (*p != NUL) 3287 ++p; 3288 eap->nextcmd = p; 3289 } 3290 else 3291 { 3292 int save_State = State; 3293 3294 // Set State to avoid the cursor shape to be set to INSERT mode 3295 // when getline() returns. 3296 State = CMDLINE; 3297 theline = eap->getline( 3298 #ifdef FEAT_EVAL 3299 eap->cstack->cs_looplevel > 0 ? -1 : 3300 #endif 3301 NUL, eap->cookie, indent, TRUE); 3302 State = save_State; 3303 } 3304 lines_left = Rows - 1; 3305 if (theline == NULL) 3306 break; 3307 3308 // Using ^ CTRL-D in getexmodeline() makes us repeat the indent. 3309 if (ex_keep_indent) 3310 append_indent = indent; 3311 3312 // Look for the "." after automatic indent. 3313 vcol = 0; 3314 for (p = theline; indent > vcol; ++p) 3315 { 3316 if (*p == ' ') 3317 ++vcol; 3318 else if (*p == TAB) 3319 vcol += 8 - vcol % 8; 3320 else 3321 break; 3322 } 3323 if ((p[0] == '.' && p[1] == NUL) 3324 || (!did_undo && u_save(lnum, lnum + 1 + (empty ? 1 : 0)) 3325 == FAIL)) 3326 { 3327 vim_free(theline); 3328 break; 3329 } 3330 3331 // don't use autoindent if nothing was typed. 3332 if (p[0] == NUL) 3333 theline[0] = NUL; 3334 3335 did_undo = TRUE; 3336 ml_append(lnum, theline, (colnr_T)0, FALSE); 3337 appended_lines_mark(lnum + (empty ? 1 : 0), 1L); 3338 3339 vim_free(theline); 3340 ++lnum; 3341 3342 if (empty) 3343 { 3344 ml_delete(2L); 3345 empty = FALSE; 3346 } 3347 } 3348 State = NORMAL; 3349 3350 if (eap->forceit) 3351 curbuf->b_p_ai = !curbuf->b_p_ai; 3352 3353 // "start" is set to eap->line2+1 unless that position is invalid (when 3354 // eap->line2 pointed to the end of the buffer and nothing was appended) 3355 // "end" is set to lnum when something has been appended, otherwise 3356 // it is the same than "start" -- Acevedo 3357 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 3358 { 3359 curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? 3360 eap->line2 + 1 : curbuf->b_ml.ml_line_count; 3361 if (eap->cmdidx != CMD_append) 3362 --curbuf->b_op_start.lnum; 3363 curbuf->b_op_end.lnum = (eap->line2 < lnum) 3364 ? lnum : curbuf->b_op_start.lnum; 3365 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 3366 } 3367 curwin->w_cursor.lnum = lnum; 3368 check_cursor_lnum(); 3369 beginline(BL_SOL | BL_FIX); 3370 3371 need_wait_return = FALSE; // don't use wait_return() now 3372 ex_no_reprint = TRUE; 3373 } 3374 3375 /* 3376 * ":change" 3377 */ 3378 void 3379 ex_change(exarg_T *eap) 3380 { 3381 linenr_T lnum; 3382 3383 #ifdef FEAT_EVAL 3384 if (not_in_vim9(eap) == FAIL) 3385 return; 3386 #endif 3387 if (eap->line2 >= eap->line1 3388 && u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) 3389 return; 3390 3391 // the ! flag toggles autoindent 3392 if (eap->forceit ? !curbuf->b_p_ai : curbuf->b_p_ai) 3393 append_indent = get_indent_lnum(eap->line1); 3394 3395 for (lnum = eap->line2; lnum >= eap->line1; --lnum) 3396 { 3397 if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete 3398 break; 3399 ml_delete(eap->line1); 3400 } 3401 3402 // make sure the cursor is not beyond the end of the file now 3403 check_cursor_lnum(); 3404 deleted_lines_mark(eap->line1, (long)(eap->line2 - lnum)); 3405 3406 // ":append" on the line above the deleted lines. 3407 eap->line2 = eap->line1; 3408 ex_append(eap); 3409 } 3410 3411 void 3412 ex_z(exarg_T *eap) 3413 { 3414 char_u *x; 3415 long bigness; 3416 char_u *kind; 3417 int minus = 0; 3418 linenr_T start, end, curs, i; 3419 int j; 3420 linenr_T lnum = eap->line2; 3421 3422 // Vi compatible: ":z!" uses display height, without a count uses 3423 // 'scroll' 3424 if (eap->forceit) 3425 bigness = curwin->w_height; 3426 else if (!ONE_WINDOW) 3427 bigness = curwin->w_height - 3; 3428 else 3429 bigness = curwin->w_p_scr * 2; 3430 if (bigness < 1) 3431 bigness = 1; 3432 3433 x = eap->arg; 3434 kind = x; 3435 if (*kind == '-' || *kind == '+' || *kind == '=' 3436 || *kind == '^' || *kind == '.') 3437 ++x; 3438 while (*x == '-' || *x == '+') 3439 ++x; 3440 3441 if (*x != 0) 3442 { 3443 if (!VIM_ISDIGIT(*x)) 3444 { 3445 emsg(_("E144: non-numeric argument to :z")); 3446 return; 3447 } 3448 else 3449 { 3450 bigness = atol((char *)x); 3451 3452 // bigness could be < 0 if atol(x) overflows. 3453 if (bigness > 2 * curbuf->b_ml.ml_line_count || bigness < 0) 3454 bigness = 2 * curbuf->b_ml.ml_line_count; 3455 3456 p_window = bigness; 3457 if (*kind == '=') 3458 bigness += 2; 3459 } 3460 } 3461 3462 // the number of '-' and '+' multiplies the distance 3463 if (*kind == '-' || *kind == '+') 3464 for (x = kind + 1; *x == *kind; ++x) 3465 ; 3466 3467 switch (*kind) 3468 { 3469 case '-': 3470 start = lnum - bigness * (linenr_T)(x - kind) + 1; 3471 end = start + bigness - 1; 3472 curs = end; 3473 break; 3474 3475 case '=': 3476 start = lnum - (bigness + 1) / 2 + 1; 3477 end = lnum + (bigness + 1) / 2 - 1; 3478 curs = lnum; 3479 minus = 1; 3480 break; 3481 3482 case '^': 3483 start = lnum - bigness * 2; 3484 end = lnum - bigness; 3485 curs = lnum - bigness; 3486 break; 3487 3488 case '.': 3489 start = lnum - (bigness + 1) / 2 + 1; 3490 end = lnum + (bigness + 1) / 2 - 1; 3491 curs = end; 3492 break; 3493 3494 default: // '+' 3495 start = lnum; 3496 if (*kind == '+') 3497 start += bigness * (linenr_T)(x - kind - 1) + 1; 3498 else if (eap->addr_count == 0) 3499 ++start; 3500 end = start + bigness - 1; 3501 curs = end; 3502 break; 3503 } 3504 3505 if (start < 1) 3506 start = 1; 3507 3508 if (end > curbuf->b_ml.ml_line_count) 3509 end = curbuf->b_ml.ml_line_count; 3510 3511 if (curs > curbuf->b_ml.ml_line_count) 3512 curs = curbuf->b_ml.ml_line_count; 3513 else if (curs < 1) 3514 curs = 1; 3515 3516 for (i = start; i <= end; i++) 3517 { 3518 if (minus && i == lnum) 3519 { 3520 msg_putchar('\n'); 3521 3522 for (j = 1; j < Columns; j++) 3523 msg_putchar('-'); 3524 } 3525 3526 print_line(i, eap->flags & EXFLAG_NR, eap->flags & EXFLAG_LIST); 3527 3528 if (minus && i == lnum) 3529 { 3530 msg_putchar('\n'); 3531 3532 for (j = 1; j < Columns; j++) 3533 msg_putchar('-'); 3534 } 3535 } 3536 3537 if (curwin->w_cursor.lnum != curs) 3538 { 3539 curwin->w_cursor.lnum = curs; 3540 curwin->w_cursor.col = 0; 3541 } 3542 ex_no_reprint = TRUE; 3543 } 3544 3545 /* 3546 * Check if the restricted flag is set. 3547 * If so, give an error message and return TRUE. 3548 * Otherwise, return FALSE. 3549 */ 3550 int 3551 check_restricted(void) 3552 { 3553 if (restricted) 3554 { 3555 emsg(_("E145: Shell commands and some functionality not allowed in rvim")); 3556 return TRUE; 3557 } 3558 return FALSE; 3559 } 3560 3561 /* 3562 * Check if the secure flag is set (.exrc or .vimrc in current directory). 3563 * If so, give an error message and return TRUE. 3564 * Otherwise, return FALSE. 3565 */ 3566 int 3567 check_secure(void) 3568 { 3569 if (secure) 3570 { 3571 secure = 2; 3572 emsg(_(e_curdir)); 3573 return TRUE; 3574 } 3575 #ifdef HAVE_SANDBOX 3576 /* 3577 * In the sandbox more things are not allowed, including the things 3578 * disallowed in secure mode. 3579 */ 3580 if (sandbox != 0) 3581 { 3582 emsg(_(e_sandbox)); 3583 return TRUE; 3584 } 3585 #endif 3586 return FALSE; 3587 } 3588 3589 static char_u *old_sub = NULL; // previous substitute pattern 3590 static int global_need_beginline; // call beginline() after ":g" 3591 3592 /* 3593 * Flags that are kept between calls to :substitute. 3594 */ 3595 typedef struct { 3596 int do_all; // do multiple substitutions per line 3597 int do_ask; // ask for confirmation 3598 int do_count; // count only 3599 int do_error; // if false, ignore errors 3600 int do_print; // print last line with subs. 3601 int do_list; // list last line with subs. 3602 int do_number; // list last line with line nr 3603 int do_ic; // ignore case flag 3604 } subflags_T; 3605 3606 /* 3607 * Perform a substitution from line eap->line1 to line eap->line2 using the 3608 * command pointed to by eap->arg which should be of the form: 3609 * 3610 * /pattern/substitution/{flags} 3611 * 3612 * The usual escapes are supported as described in the regexp docs. 3613 */ 3614 void 3615 ex_substitute(exarg_T *eap) 3616 { 3617 linenr_T lnum; 3618 long i = 0; 3619 regmmatch_T regmatch; 3620 static subflags_T subflags = {FALSE, FALSE, FALSE, TRUE, FALSE, 3621 FALSE, FALSE, 0}; 3622 #ifdef FEAT_EVAL 3623 subflags_T subflags_save; 3624 #endif 3625 int save_do_all; // remember user specified 'g' flag 3626 int save_do_ask; // remember user specified 'c' flag 3627 char_u *pat = NULL, *sub = NULL; // init for GCC 3628 int delimiter; 3629 int sublen; 3630 int got_quit = FALSE; 3631 int got_match = FALSE; 3632 int temp; 3633 int which_pat; 3634 char_u *cmd; 3635 int save_State; 3636 linenr_T first_line = 0; // first changed line 3637 linenr_T last_line= 0; // below last changed line AFTER the 3638 // change 3639 linenr_T old_line_count = curbuf->b_ml.ml_line_count; 3640 linenr_T line2; 3641 long nmatch; // number of lines in match 3642 char_u *sub_firstline; // allocated copy of first sub line 3643 int endcolumn = FALSE; // cursor in last column when done 3644 pos_T old_cursor = curwin->w_cursor; 3645 int start_nsubs; 3646 #ifdef FEAT_EVAL 3647 int save_ma = 0; 3648 #endif 3649 3650 cmd = eap->arg; 3651 if (!global_busy) 3652 { 3653 sub_nsubs = 0; 3654 sub_nlines = 0; 3655 } 3656 start_nsubs = sub_nsubs; 3657 3658 if (eap->cmdidx == CMD_tilde) 3659 which_pat = RE_LAST; // use last used regexp 3660 else 3661 which_pat = RE_SUBST; // use last substitute regexp 3662 3663 // new pattern and substitution 3664 if (eap->cmd[0] == 's' && *cmd != NUL && !VIM_ISWHITE(*cmd) 3665 && vim_strchr((char_u *)"0123456789cegriIp|\"", *cmd) == NULL) 3666 { 3667 // don't accept alphanumeric for separator 3668 if (isalpha(*cmd)) 3669 { 3670 emsg(_("E146: Regular expressions can't be delimited by letters")); 3671 return; 3672 } 3673 /* 3674 * undocumented vi feature: 3675 * "\/sub/" and "\?sub?" use last used search pattern (almost like 3676 * //sub/r). "\&sub&" use last substitute pattern (like //sub/). 3677 */ 3678 if (*cmd == '\\') 3679 { 3680 ++cmd; 3681 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) 3682 { 3683 emsg(_(e_backslash)); 3684 return; 3685 } 3686 if (*cmd != '&') 3687 which_pat = RE_SEARCH; // use last '/' pattern 3688 pat = (char_u *)""; // empty search pattern 3689 delimiter = *cmd++; // remember delimiter character 3690 } 3691 else // find the end of the regexp 3692 { 3693 which_pat = RE_LAST; // use last used regexp 3694 delimiter = *cmd++; // remember delimiter character 3695 pat = cmd; // remember start of search pat 3696 cmd = skip_regexp_ex(cmd, delimiter, magic_isset(), 3697 &eap->arg, NULL, NULL); 3698 if (cmd[0] == delimiter) // end delimiter found 3699 *cmd++ = NUL; // replace it with a NUL 3700 } 3701 3702 /* 3703 * Small incompatibility: vi sees '\n' as end of the command, but in 3704 * Vim we want to use '\n' to find/substitute a NUL. 3705 */ 3706 sub = cmd; // remember the start of the substitution 3707 3708 while (cmd[0]) 3709 { 3710 if (cmd[0] == delimiter) // end delimiter found 3711 { 3712 *cmd++ = NUL; // replace it with a NUL 3713 break; 3714 } 3715 if (cmd[0] == '\\' && cmd[1] != 0) // skip escaped characters 3716 ++cmd; 3717 MB_PTR_ADV(cmd); 3718 } 3719 3720 if (!eap->skip) 3721 { 3722 // In POSIX vi ":s/pat/%/" uses the previous subst. string. 3723 if (STRCMP(sub, "%") == 0 3724 && vim_strchr(p_cpo, CPO_SUBPERCENT) != NULL) 3725 { 3726 if (old_sub == NULL) // there is no previous command 3727 { 3728 emsg(_(e_nopresub)); 3729 return; 3730 } 3731 sub = old_sub; 3732 } 3733 else 3734 { 3735 vim_free(old_sub); 3736 old_sub = vim_strsave(sub); 3737 } 3738 } 3739 } 3740 else if (!eap->skip) // use previous pattern and substitution 3741 { 3742 if (old_sub == NULL) // there is no previous command 3743 { 3744 emsg(_(e_nopresub)); 3745 return; 3746 } 3747 pat = NULL; // search_regcomp() will use previous pattern 3748 sub = old_sub; 3749 3750 // Vi compatibility quirk: repeating with ":s" keeps the cursor in the 3751 // last column after using "$". 3752 endcolumn = (curwin->w_curswant == MAXCOL); 3753 } 3754 3755 // Recognize ":%s/\n//" and turn it into a join command, which is much 3756 // more efficient. 3757 // TODO: find a generic solution to make line-joining operations more 3758 // efficient, avoid allocating a string that grows in size. 3759 if (pat != NULL && STRCMP(pat, "\\n") == 0 3760 && *sub == NUL 3761 && (*cmd == NUL || (cmd[1] == NUL && (*cmd == 'g' || *cmd == 'l' 3762 || *cmd == 'p' || *cmd == '#')))) 3763 { 3764 linenr_T joined_lines_count; 3765 3766 if (eap->skip) 3767 return; 3768 curwin->w_cursor.lnum = eap->line1; 3769 if (*cmd == 'l') 3770 eap->flags = EXFLAG_LIST; 3771 else if (*cmd == '#') 3772 eap->flags = EXFLAG_NR; 3773 else if (*cmd == 'p') 3774 eap->flags = EXFLAG_PRINT; 3775 3776 // The number of lines joined is the number of lines in the range plus 3777 // one. One less when the last line is included. 3778 joined_lines_count = eap->line2 - eap->line1 + 1; 3779 if (eap->line2 < curbuf->b_ml.ml_line_count) 3780 ++joined_lines_count; 3781 if (joined_lines_count > 1) 3782 { 3783 (void)do_join(joined_lines_count, FALSE, TRUE, FALSE, TRUE); 3784 sub_nsubs = joined_lines_count - 1; 3785 sub_nlines = 1; 3786 (void)do_sub_msg(FALSE); 3787 ex_may_print(eap); 3788 } 3789 3790 if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0) 3791 save_re_pat(RE_SUBST, pat, magic_isset()); 3792 // put pattern in history 3793 add_to_history(HIST_SEARCH, pat, TRUE, NUL); 3794 3795 return; 3796 } 3797 3798 /* 3799 * Find trailing options. When '&' is used, keep old options. 3800 */ 3801 if (*cmd == '&') 3802 ++cmd; 3803 else 3804 { 3805 #ifdef FEAT_EVAL 3806 if (in_vim9script()) 3807 { 3808 // ignore 'gdefault' and 'edcompatible' 3809 subflags.do_all = FALSE; 3810 subflags.do_ask = FALSE; 3811 } 3812 else 3813 #endif 3814 if (!p_ed) 3815 { 3816 if (p_gd) // default is global on 3817 subflags.do_all = TRUE; 3818 else 3819 subflags.do_all = FALSE; 3820 subflags.do_ask = FALSE; 3821 } 3822 subflags.do_error = TRUE; 3823 subflags.do_print = FALSE; 3824 subflags.do_list = FALSE; 3825 subflags.do_count = FALSE; 3826 subflags.do_number = FALSE; 3827 subflags.do_ic = 0; 3828 } 3829 while (*cmd) 3830 { 3831 /* 3832 * Note that 'g' and 'c' are always inverted, also when p_ed is off. 3833 * 'r' is never inverted. 3834 */ 3835 if (*cmd == 'g') 3836 subflags.do_all = !subflags.do_all; 3837 else if (*cmd == 'c') 3838 subflags.do_ask = !subflags.do_ask; 3839 else if (*cmd == 'n') 3840 subflags.do_count = TRUE; 3841 else if (*cmd == 'e') 3842 subflags.do_error = !subflags.do_error; 3843 else if (*cmd == 'r') // use last used regexp 3844 which_pat = RE_LAST; 3845 else if (*cmd == 'p') 3846 subflags.do_print = TRUE; 3847 else if (*cmd == '#') 3848 { 3849 subflags.do_print = TRUE; 3850 subflags.do_number = TRUE; 3851 } 3852 else if (*cmd == 'l') 3853 { 3854 subflags.do_print = TRUE; 3855 subflags.do_list = TRUE; 3856 } 3857 else if (*cmd == 'i') // ignore case 3858 subflags.do_ic = 'i'; 3859 else if (*cmd == 'I') // don't ignore case 3860 subflags.do_ic = 'I'; 3861 else 3862 break; 3863 ++cmd; 3864 } 3865 if (subflags.do_count) 3866 subflags.do_ask = FALSE; 3867 3868 save_do_all = subflags.do_all; 3869 save_do_ask = subflags.do_ask; 3870 3871 /* 3872 * check for a trailing count 3873 */ 3874 cmd = skipwhite(cmd); 3875 if (VIM_ISDIGIT(*cmd)) 3876 { 3877 i = getdigits(&cmd); 3878 if (i <= 0 && !eap->skip && subflags.do_error) 3879 { 3880 emsg(_(e_zerocount)); 3881 return; 3882 } 3883 eap->line1 = eap->line2; 3884 eap->line2 += i - 1; 3885 if (eap->line2 > curbuf->b_ml.ml_line_count) 3886 eap->line2 = curbuf->b_ml.ml_line_count; 3887 } 3888 3889 /* 3890 * check for trailing command or garbage 3891 */ 3892 cmd = skipwhite(cmd); 3893 if (*cmd && *cmd != '"') // if not end-of-line or comment 3894 { 3895 eap->nextcmd = check_nextcmd(cmd); 3896 if (eap->nextcmd == NULL) 3897 { 3898 semsg(_(e_trailing_arg), cmd); 3899 return; 3900 } 3901 } 3902 3903 if (eap->skip) // not executing commands, only parsing 3904 return; 3905 3906 if (!subflags.do_count && !curbuf->b_p_ma) 3907 { 3908 // Substitution is not allowed in non-'modifiable' buffer 3909 emsg(_(e_modifiable)); 3910 return; 3911 } 3912 3913 if (search_regcomp(pat, RE_SUBST, which_pat, SEARCH_HIS, ®match) == FAIL) 3914 { 3915 if (subflags.do_error) 3916 emsg(_(e_invalid_command)); 3917 return; 3918 } 3919 3920 // the 'i' or 'I' flag overrules 'ignorecase' and 'smartcase' 3921 if (subflags.do_ic == 'i') 3922 regmatch.rmm_ic = TRUE; 3923 else if (subflags.do_ic == 'I') 3924 regmatch.rmm_ic = FALSE; 3925 3926 sub_firstline = NULL; 3927 3928 /* 3929 * ~ in the substitute pattern is replaced with the old pattern. 3930 * We do it here once to avoid it to be replaced over and over again. 3931 * But don't do it when it starts with "\=", then it's an expression. 3932 */ 3933 if (!(sub[0] == '\\' && sub[1] == '=')) 3934 sub = regtilde(sub, magic_isset()); 3935 3936 /* 3937 * Check for a match on each line. 3938 */ 3939 line2 = eap->line2; 3940 for (lnum = eap->line1; lnum <= line2 && !(got_quit 3941 #if defined(FEAT_EVAL) 3942 || aborting() 3943 #endif 3944 ); ++lnum) 3945 { 3946 nmatch = vim_regexec_multi(®match, curwin, curbuf, lnum, 3947 (colnr_T)0, NULL, NULL); 3948 if (nmatch) 3949 { 3950 colnr_T copycol; 3951 colnr_T matchcol; 3952 colnr_T prev_matchcol = MAXCOL; 3953 char_u *new_end, *new_start = NULL; 3954 unsigned new_start_len = 0; 3955 char_u *p1; 3956 int did_sub = FALSE; 3957 int lastone; 3958 int len, copy_len, needed_len; 3959 long nmatch_tl = 0; // nr of lines matched below lnum 3960 int do_again; // do it again after joining lines 3961 int skip_match = FALSE; 3962 linenr_T sub_firstlnum; // nr of first sub line 3963 #ifdef FEAT_PROP_POPUP 3964 int apc_flags = APC_SAVE_FOR_UNDO | APC_SUBSTITUTE; 3965 colnr_T total_added = 0; 3966 #endif 3967 3968 /* 3969 * The new text is build up step by step, to avoid too much 3970 * copying. There are these pieces: 3971 * sub_firstline The old text, unmodified. 3972 * copycol Column in the old text where we started 3973 * looking for a match; from here old text still 3974 * needs to be copied to the new text. 3975 * matchcol Column number of the old text where to look 3976 * for the next match. It's just after the 3977 * previous match or one further. 3978 * prev_matchcol Column just after the previous match (if any). 3979 * Mostly equal to matchcol, except for the first 3980 * match and after skipping an empty match. 3981 * regmatch.*pos Where the pattern matched in the old text. 3982 * new_start The new text, all that has been produced so 3983 * far. 3984 * new_end The new text, where to append new text. 3985 * 3986 * lnum The line number where we found the start of 3987 * the match. Can be below the line we searched 3988 * when there is a \n before a \zs in the 3989 * pattern. 3990 * sub_firstlnum The line number in the buffer where to look 3991 * for a match. Can be different from "lnum" 3992 * when the pattern or substitute string contains 3993 * line breaks. 3994 * 3995 * Special situations: 3996 * - When the substitute string contains a line break, the part up 3997 * to the line break is inserted in the text, but the copy of 3998 * the original line is kept. "sub_firstlnum" is adjusted for 3999 * the inserted lines. 4000 * - When the matched pattern contains a line break, the old line 4001 * is taken from the line at the end of the pattern. The lines 4002 * in the match are deleted later, "sub_firstlnum" is adjusted 4003 * accordingly. 4004 * 4005 * The new text is built up in new_start[]. It has some extra 4006 * room to avoid using alloc()/free() too often. new_start_len is 4007 * the length of the allocated memory at new_start. 4008 * 4009 * Make a copy of the old line, so it won't be taken away when 4010 * updating the screen or handling a multi-line match. The "old_" 4011 * pointers point into this copy. 4012 */ 4013 sub_firstlnum = lnum; 4014 copycol = 0; 4015 matchcol = 0; 4016 4017 // At first match, remember current cursor position. 4018 if (!got_match) 4019 { 4020 setpcmark(); 4021 got_match = TRUE; 4022 } 4023 4024 /* 4025 * Loop until nothing more to replace in this line. 4026 * 1. Handle match with empty string. 4027 * 2. If do_ask is set, ask for confirmation. 4028 * 3. substitute the string. 4029 * 4. if do_all is set, find next match 4030 * 5. break if there isn't another match in this line 4031 */ 4032 for (;;) 4033 { 4034 // Advance "lnum" to the line where the match starts. The 4035 // match does not start in the first line when there is a line 4036 // break before \zs. 4037 if (regmatch.startpos[0].lnum > 0) 4038 { 4039 lnum += regmatch.startpos[0].lnum; 4040 sub_firstlnum += regmatch.startpos[0].lnum; 4041 nmatch -= regmatch.startpos[0].lnum; 4042 VIM_CLEAR(sub_firstline); 4043 } 4044 4045 // Match might be after the last line for "\n\zs" matching at 4046 // the end of the last line. 4047 if (lnum > curbuf->b_ml.ml_line_count) 4048 break; 4049 4050 if (sub_firstline == NULL) 4051 { 4052 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); 4053 if (sub_firstline == NULL) 4054 { 4055 vim_free(new_start); 4056 goto outofmem; 4057 } 4058 } 4059 4060 // Save the line number of the last change for the final 4061 // cursor position (just like Vi). 4062 curwin->w_cursor.lnum = lnum; 4063 do_again = FALSE; 4064 4065 /* 4066 * 1. Match empty string does not count, except for first 4067 * match. This reproduces the strange vi behaviour. 4068 * This also catches endless loops. 4069 */ 4070 if (matchcol == prev_matchcol 4071 && regmatch.endpos[0].lnum == 0 4072 && matchcol == regmatch.endpos[0].col) 4073 { 4074 if (sub_firstline[matchcol] == NUL) 4075 // We already were at the end of the line. Don't look 4076 // for a match in this line again. 4077 skip_match = TRUE; 4078 else 4079 { 4080 // search for a match at next column 4081 if (has_mbyte) 4082 matchcol += mb_ptr2len(sub_firstline + matchcol); 4083 else 4084 ++matchcol; 4085 } 4086 goto skip; 4087 } 4088 4089 // Normally we continue searching for a match just after the 4090 // previous match. 4091 matchcol = regmatch.endpos[0].col; 4092 prev_matchcol = matchcol; 4093 4094 /* 4095 * 2. If do_count is set only increase the counter. 4096 * If do_ask is set, ask for confirmation. 4097 */ 4098 if (subflags.do_count) 4099 { 4100 // For a multi-line match, put matchcol at the NUL at 4101 // the end of the line and set nmatch to one, so that 4102 // we continue looking for a match on the next line. 4103 // Avoids that ":s/\nB\@=//gc" get stuck. 4104 if (nmatch > 1) 4105 { 4106 matchcol = (colnr_T)STRLEN(sub_firstline); 4107 nmatch = 1; 4108 skip_match = TRUE; 4109 } 4110 sub_nsubs++; 4111 did_sub = TRUE; 4112 #ifdef FEAT_EVAL 4113 // Skip the substitution, unless an expression is used, 4114 // then it is evaluated in the sandbox. 4115 if (!(sub[0] == '\\' && sub[1] == '=')) 4116 #endif 4117 goto skip; 4118 } 4119 4120 if (subflags.do_ask) 4121 { 4122 int typed = 0; 4123 4124 // change State to CONFIRM, so that the mouse works 4125 // properly 4126 save_State = State; 4127 State = CONFIRM; 4128 setmouse(); // disable mouse in xterm 4129 curwin->w_cursor.col = regmatch.startpos[0].col; 4130 if (curwin->w_p_crb) 4131 do_check_cursorbind(); 4132 4133 // When 'cpoptions' contains "u" don't sync undo when 4134 // asking for confirmation. 4135 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) 4136 ++no_u_sync; 4137 4138 /* 4139 * Loop until 'y', 'n', 'q', CTRL-E or CTRL-Y typed. 4140 */ 4141 while (subflags.do_ask) 4142 { 4143 if (exmode_active) 4144 { 4145 char_u *resp; 4146 colnr_T sc, ec; 4147 4148 print_line_no_prefix(lnum, 4149 subflags.do_number, subflags.do_list); 4150 4151 getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); 4152 curwin->w_cursor.col = regmatch.endpos[0].col - 1; 4153 if (curwin->w_cursor.col < 0) 4154 curwin->w_cursor.col = 0; 4155 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); 4156 if (subflags.do_number || curwin->w_p_nu) 4157 { 4158 int numw = number_width(curwin) + 1; 4159 sc += numw; 4160 ec += numw; 4161 } 4162 msg_start(); 4163 for (i = 0; i < (long)sc; ++i) 4164 msg_putchar(' '); 4165 for ( ; i <= (long)ec; ++i) 4166 msg_putchar('^'); 4167 4168 resp = getexmodeline('?', NULL, 0, TRUE); 4169 if (resp != NULL) 4170 { 4171 typed = *resp; 4172 vim_free(resp); 4173 } 4174 } 4175 else 4176 { 4177 char_u *orig_line = NULL; 4178 int len_change = 0; 4179 int save_p_lz = p_lz; 4180 #ifdef FEAT_FOLDING 4181 int save_p_fen = curwin->w_p_fen; 4182 4183 curwin->w_p_fen = FALSE; 4184 #endif 4185 // Invert the matched string. 4186 // Remove the inversion afterwards. 4187 temp = RedrawingDisabled; 4188 RedrawingDisabled = 0; 4189 4190 // avoid calling update_screen() in vgetorpeek() 4191 p_lz = FALSE; 4192 4193 if (new_start != NULL) 4194 { 4195 // There already was a substitution, we would 4196 // like to show this to the user. We cannot 4197 // really update the line, it would change 4198 // what matches. Temporarily replace the line 4199 // and change it back afterwards. 4200 orig_line = vim_strsave(ml_get(lnum)); 4201 if (orig_line != NULL) 4202 { 4203 char_u *new_line = concat_str(new_start, 4204 sub_firstline + copycol); 4205 4206 if (new_line == NULL) 4207 VIM_CLEAR(orig_line); 4208 else 4209 { 4210 // Position the cursor relative to the 4211 // end of the line, the previous 4212 // substitute may have inserted or 4213 // deleted characters before the 4214 // cursor. 4215 len_change = (int)STRLEN(new_line) 4216 - (int)STRLEN(orig_line); 4217 curwin->w_cursor.col += len_change; 4218 ml_replace(lnum, new_line, FALSE); 4219 } 4220 } 4221 } 4222 4223 search_match_lines = regmatch.endpos[0].lnum 4224 - regmatch.startpos[0].lnum; 4225 search_match_endcol = regmatch.endpos[0].col 4226 + len_change; 4227 highlight_match = TRUE; 4228 4229 update_topline(); 4230 validate_cursor(); 4231 update_screen(SOME_VALID); 4232 highlight_match = FALSE; 4233 redraw_later(SOME_VALID); 4234 4235 #ifdef FEAT_FOLDING 4236 curwin->w_p_fen = save_p_fen; 4237 #endif 4238 if (msg_row == Rows - 1) 4239 msg_didout = FALSE; // avoid a scroll-up 4240 msg_starthere(); 4241 i = msg_scroll; 4242 msg_scroll = 0; // truncate msg when 4243 // needed 4244 msg_no_more = TRUE; 4245 // write message same highlighting as for 4246 // wait_return 4247 smsg_attr(HL_ATTR(HLF_R), 4248 _("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); 4249 msg_no_more = FALSE; 4250 msg_scroll = i; 4251 showruler(TRUE); 4252 windgoto(msg_row, msg_col); 4253 RedrawingDisabled = temp; 4254 4255 #ifdef USE_ON_FLY_SCROLL 4256 dont_scroll = FALSE; // allow scrolling here 4257 #endif 4258 ++no_mapping; // don't map this key 4259 ++allow_keys; // allow special keys 4260 typed = plain_vgetc(); 4261 --allow_keys; 4262 --no_mapping; 4263 4264 // clear the question 4265 msg_didout = FALSE; // don't scroll up 4266 msg_col = 0; 4267 gotocmdline(TRUE); 4268 p_lz = save_p_lz; 4269 4270 // restore the line 4271 if (orig_line != NULL) 4272 ml_replace(lnum, orig_line, FALSE); 4273 } 4274 4275 need_wait_return = FALSE; // no hit-return prompt 4276 if (typed == 'q' || typed == ESC || typed == Ctrl_C 4277 #ifdef UNIX 4278 || typed == intr_char 4279 #endif 4280 ) 4281 { 4282 got_quit = TRUE; 4283 break; 4284 } 4285 if (typed == 'n') 4286 break; 4287 if (typed == 'y') 4288 break; 4289 if (typed == 'l') 4290 { 4291 // last: replace and then stop 4292 subflags.do_all = FALSE; 4293 line2 = lnum; 4294 break; 4295 } 4296 if (typed == 'a') 4297 { 4298 subflags.do_ask = FALSE; 4299 break; 4300 } 4301 if (typed == Ctrl_E) 4302 scrollup_clamp(); 4303 else if (typed == Ctrl_Y) 4304 scrolldown_clamp(); 4305 } 4306 State = save_State; 4307 setmouse(); 4308 if (vim_strchr(p_cpo, CPO_UNDO) != NULL) 4309 --no_u_sync; 4310 4311 if (typed == 'n') 4312 { 4313 // For a multi-line match, put matchcol at the NUL at 4314 // the end of the line and set nmatch to one, so that 4315 // we continue looking for a match on the next line. 4316 // Avoids that ":%s/\nB\@=//gc" and ":%s/\n/,\r/gc" 4317 // get stuck when pressing 'n'. 4318 if (nmatch > 1) 4319 { 4320 matchcol = (colnr_T)STRLEN(sub_firstline); 4321 skip_match = TRUE; 4322 } 4323 goto skip; 4324 } 4325 if (got_quit) 4326 goto skip; 4327 } 4328 4329 // Move the cursor to the start of the match, so that we can 4330 // use "\=col("."). 4331 curwin->w_cursor.col = regmatch.startpos[0].col; 4332 4333 /* 4334 * 3. substitute the string. 4335 */ 4336 #ifdef FEAT_EVAL 4337 save_ma = curbuf->b_p_ma; 4338 if (subflags.do_count) 4339 { 4340 // prevent accidentally changing the buffer by a function 4341 curbuf->b_p_ma = FALSE; 4342 sandbox++; 4343 } 4344 // Save flags for recursion. They can change for e.g. 4345 // :s/^/\=execute("s#^##gn") 4346 subflags_save = subflags; 4347 #endif 4348 // get length of substitution part 4349 sublen = vim_regsub_multi(®match, 4350 sub_firstlnum - regmatch.startpos[0].lnum, 4351 sub, sub_firstline, FALSE, magic_isset(), TRUE); 4352 #ifdef FEAT_EVAL 4353 // If getting the substitute string caused an error, don't do 4354 // the replacement. 4355 // Don't keep flags set by a recursive call. 4356 subflags = subflags_save; 4357 if (aborting() || subflags.do_count) 4358 { 4359 curbuf->b_p_ma = save_ma; 4360 if (sandbox > 0) 4361 sandbox--; 4362 goto skip; 4363 } 4364 #endif 4365 4366 // When the match included the "$" of the last line it may 4367 // go beyond the last line of the buffer. 4368 if (nmatch > curbuf->b_ml.ml_line_count - sub_firstlnum + 1) 4369 { 4370 nmatch = curbuf->b_ml.ml_line_count - sub_firstlnum + 1; 4371 skip_match = TRUE; 4372 } 4373 4374 // Need room for: 4375 // - result so far in new_start (not for first sub in line) 4376 // - original text up to match 4377 // - length of substituted part 4378 // - original text after match 4379 // Adjust text properties here, since we have all information 4380 // needed. 4381 if (nmatch == 1) 4382 { 4383 p1 = sub_firstline; 4384 #ifdef FEAT_PROP_POPUP 4385 if (curbuf->b_has_textprop) 4386 { 4387 int bytes_added = sublen - 1 - (regmatch.endpos[0].col 4388 - regmatch.startpos[0].col); 4389 4390 // When text properties are changed, need to save for 4391 // undo first, unless done already. 4392 if (adjust_prop_columns(lnum, 4393 total_added + regmatch.startpos[0].col, 4394 bytes_added, apc_flags)) 4395 apc_flags &= ~APC_SAVE_FOR_UNDO; 4396 // Offset for column byte number of the text property 4397 // in the resulting buffer afterwards. 4398 total_added += bytes_added; 4399 } 4400 #endif 4401 } 4402 else 4403 { 4404 p1 = ml_get(sub_firstlnum + nmatch - 1); 4405 nmatch_tl += nmatch - 1; 4406 } 4407 copy_len = regmatch.startpos[0].col - copycol; 4408 needed_len = copy_len + ((unsigned)STRLEN(p1) 4409 - regmatch.endpos[0].col) + sublen + 1; 4410 if (new_start == NULL) 4411 { 4412 /* 4413 * Get some space for a temporary buffer to do the 4414 * substitution into (and some extra space to avoid 4415 * too many calls to alloc()/free()). 4416 */ 4417 new_start_len = needed_len + 50; 4418 if ((new_start = alloc(new_start_len)) == NULL) 4419 goto outofmem; 4420 *new_start = NUL; 4421 new_end = new_start; 4422 } 4423 else 4424 { 4425 /* 4426 * Check if the temporary buffer is long enough to do the 4427 * substitution into. If not, make it larger (with a bit 4428 * extra to avoid too many calls to alloc()/free()). 4429 */ 4430 len = (unsigned)STRLEN(new_start); 4431 needed_len += len; 4432 if (needed_len > (int)new_start_len) 4433 { 4434 new_start_len = needed_len + 50; 4435 if ((p1 = alloc(new_start_len)) == NULL) 4436 { 4437 vim_free(new_start); 4438 goto outofmem; 4439 } 4440 mch_memmove(p1, new_start, (size_t)(len + 1)); 4441 vim_free(new_start); 4442 new_start = p1; 4443 } 4444 new_end = new_start + len; 4445 } 4446 4447 /* 4448 * copy the text up to the part that matched 4449 */ 4450 mch_memmove(new_end, sub_firstline + copycol, (size_t)copy_len); 4451 new_end += copy_len; 4452 4453 (void)vim_regsub_multi(®match, 4454 sub_firstlnum - regmatch.startpos[0].lnum, 4455 sub, new_end, TRUE, magic_isset(), TRUE); 4456 sub_nsubs++; 4457 did_sub = TRUE; 4458 4459 // Move the cursor to the start of the line, to avoid that it 4460 // is beyond the end of the line after the substitution. 4461 curwin->w_cursor.col = 0; 4462 4463 // For a multi-line match, make a copy of the last matched 4464 // line and continue in that one. 4465 if (nmatch > 1) 4466 { 4467 sub_firstlnum += nmatch - 1; 4468 vim_free(sub_firstline); 4469 sub_firstline = vim_strsave(ml_get(sub_firstlnum)); 4470 // When going beyond the last line, stop substituting. 4471 if (sub_firstlnum <= line2) 4472 do_again = TRUE; 4473 else 4474 subflags.do_all = FALSE; 4475 } 4476 4477 // Remember next character to be copied. 4478 copycol = regmatch.endpos[0].col; 4479 4480 if (skip_match) 4481 { 4482 // Already hit end of the buffer, sub_firstlnum is one 4483 // less than what it ought to be. 4484 vim_free(sub_firstline); 4485 sub_firstline = vim_strsave((char_u *)""); 4486 copycol = 0; 4487 } 4488 4489 /* 4490 * Now the trick is to replace CTRL-M chars with a real line 4491 * break. This would make it impossible to insert a CTRL-M in 4492 * the text. The line break can be avoided by preceding the 4493 * CTRL-M with a backslash. To be able to insert a backslash, 4494 * they must be doubled in the string and are halved here. 4495 * That is Vi compatible. 4496 */ 4497 for (p1 = new_end; *p1; ++p1) 4498 { 4499 if (p1[0] == '\\' && p1[1] != NUL) // remove backslash 4500 { 4501 STRMOVE(p1, p1 + 1); 4502 #ifdef FEAT_PROP_POPUP 4503 if (curbuf->b_has_textprop) 4504 { 4505 // When text properties are changed, need to save 4506 // for undo first, unless done already. 4507 if (adjust_prop_columns(lnum, 4508 (colnr_T)(p1 - new_start), -1, 4509 apc_flags)) 4510 apc_flags &= ~APC_SAVE_FOR_UNDO; 4511 } 4512 #endif 4513 } 4514 else if (*p1 == CAR) 4515 { 4516 if (u_inssub(lnum) == OK) // prepare for undo 4517 { 4518 colnr_T plen = (colnr_T)(p1 - new_start + 1); 4519 4520 *p1 = NUL; // truncate up to the CR 4521 ml_append(lnum - 1, new_start, plen, FALSE); 4522 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); 4523 if (subflags.do_ask) 4524 appended_lines(lnum - 1, 1L); 4525 else 4526 { 4527 if (first_line == 0) 4528 first_line = lnum; 4529 last_line = lnum + 1; 4530 } 4531 #ifdef FEAT_PROP_POPUP 4532 adjust_props_for_split(lnum + 1, lnum, plen, 1); 4533 #endif 4534 // all line numbers increase 4535 ++sub_firstlnum; 4536 ++lnum; 4537 ++line2; 4538 // move the cursor to the new line, like Vi 4539 ++curwin->w_cursor.lnum; 4540 // copy the rest 4541 STRMOVE(new_start, p1 + 1); 4542 p1 = new_start - 1; 4543 } 4544 } 4545 else if (has_mbyte) 4546 p1 += (*mb_ptr2len)(p1) - 1; 4547 } 4548 4549 /* 4550 * 4. If do_all is set, find next match. 4551 * Prevent endless loop with patterns that match empty 4552 * strings, e.g. :s/$/pat/g or :s/[a-z]* /(&)/g. 4553 * But ":s/\n/#/" is OK. 4554 */ 4555 skip: 4556 // We already know that we did the last subst when we are at 4557 // the end of the line, except that a pattern like 4558 // "bar\|\nfoo" may match at the NUL. "lnum" can be below 4559 // "line2" when there is a \zs in the pattern after a line 4560 // break. 4561 lastone = (skip_match 4562 || got_int 4563 || got_quit 4564 || lnum > line2 4565 || !(subflags.do_all || do_again) 4566 || (sub_firstline[matchcol] == NUL && nmatch <= 1 4567 && !re_multiline(regmatch.regprog))); 4568 nmatch = -1; 4569 4570 /* 4571 * Replace the line in the buffer when needed. This is 4572 * skipped when there are more matches. 4573 * The check for nmatch_tl is needed for when multi-line 4574 * matching must replace the lines before trying to do another 4575 * match, otherwise "\@<=" won't work. 4576 * When the match starts below where we start searching also 4577 * need to replace the line first (using \zs after \n). 4578 */ 4579 if (lastone 4580 || nmatch_tl > 0 4581 || (nmatch = vim_regexec_multi(®match, curwin, 4582 curbuf, sub_firstlnum, 4583 matchcol, NULL, NULL)) == 0 4584 || regmatch.startpos[0].lnum > 0) 4585 { 4586 if (new_start != NULL) 4587 { 4588 /* 4589 * Copy the rest of the line, that didn't match. 4590 * "matchcol" has to be adjusted, we use the end of 4591 * the line as reference, because the substitute may 4592 * have changed the number of characters. Same for 4593 * "prev_matchcol". 4594 */ 4595 STRCAT(new_start, sub_firstline + copycol); 4596 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; 4597 prev_matchcol = (colnr_T)STRLEN(sub_firstline) 4598 - prev_matchcol; 4599 4600 if (u_savesub(lnum) != OK) 4601 break; 4602 ml_replace(lnum, new_start, TRUE); 4603 4604 if (nmatch_tl > 0) 4605 { 4606 /* 4607 * Matched lines have now been substituted and are 4608 * useless, delete them. The part after the match 4609 * has been appended to new_start, we don't need 4610 * it in the buffer. 4611 */ 4612 ++lnum; 4613 if (u_savedel(lnum, nmatch_tl) != OK) 4614 break; 4615 for (i = 0; i < nmatch_tl; ++i) 4616 ml_delete(lnum); 4617 mark_adjust(lnum, lnum + nmatch_tl - 1, 4618 (long)MAXLNUM, -nmatch_tl); 4619 if (subflags.do_ask) 4620 deleted_lines(lnum, nmatch_tl); 4621 --lnum; 4622 line2 -= nmatch_tl; // nr of lines decreases 4623 nmatch_tl = 0; 4624 } 4625 4626 // When asking, undo is saved each time, must also set 4627 // changed flag each time. 4628 if (subflags.do_ask) 4629 changed_bytes(lnum, 0); 4630 else 4631 { 4632 if (first_line == 0) 4633 first_line = lnum; 4634 last_line = lnum + 1; 4635 } 4636 4637 sub_firstlnum = lnum; 4638 vim_free(sub_firstline); // free the temp buffer 4639 sub_firstline = new_start; 4640 new_start = NULL; 4641 matchcol = (colnr_T)STRLEN(sub_firstline) - matchcol; 4642 prev_matchcol = (colnr_T)STRLEN(sub_firstline) 4643 - prev_matchcol; 4644 copycol = 0; 4645 } 4646 if (nmatch == -1 && !lastone) 4647 nmatch = vim_regexec_multi(®match, curwin, curbuf, 4648 sub_firstlnum, matchcol, NULL, NULL); 4649 4650 /* 4651 * 5. break if there isn't another match in this line 4652 */ 4653 if (nmatch <= 0) 4654 { 4655 // If the match found didn't start where we were 4656 // searching, do the next search in the line where we 4657 // found the match. 4658 if (nmatch == -1) 4659 lnum -= regmatch.startpos[0].lnum; 4660 break; 4661 } 4662 } 4663 4664 line_breakcheck(); 4665 } 4666 4667 if (did_sub) 4668 ++sub_nlines; 4669 vim_free(new_start); // for when substitute was cancelled 4670 VIM_CLEAR(sub_firstline); // free the copy of the original line 4671 } 4672 4673 line_breakcheck(); 4674 } 4675 4676 if (first_line != 0) 4677 { 4678 // Need to subtract the number of added lines from "last_line" to get 4679 // the line number before the change (same as adding the number of 4680 // deleted lines). 4681 i = curbuf->b_ml.ml_line_count - old_line_count; 4682 changed_lines(first_line, 0, last_line - i, i); 4683 } 4684 4685 outofmem: 4686 vim_free(sub_firstline); // may have to free allocated copy of the line 4687 4688 // ":s/pat//n" doesn't move the cursor 4689 if (subflags.do_count) 4690 curwin->w_cursor = old_cursor; 4691 4692 if (sub_nsubs > start_nsubs) 4693 { 4694 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) 4695 { 4696 // Set the '[ and '] marks. 4697 curbuf->b_op_start.lnum = eap->line1; 4698 curbuf->b_op_end.lnum = line2; 4699 curbuf->b_op_start.col = curbuf->b_op_end.col = 0; 4700 } 4701 4702 if (!global_busy) 4703 { 4704 // when interactive leave cursor on the match 4705 if (!subflags.do_ask) 4706 { 4707 if (endcolumn) 4708 coladvance((colnr_T)MAXCOL); 4709 else 4710 beginline(BL_WHITE | BL_FIX); 4711 } 4712 if (!do_sub_msg(subflags.do_count) && subflags.do_ask) 4713 msg(""); 4714 } 4715 else 4716 global_need_beginline = TRUE; 4717 if (subflags.do_print) 4718 print_line(curwin->w_cursor.lnum, 4719 subflags.do_number, subflags.do_list); 4720 } 4721 else if (!global_busy) 4722 { 4723 if (got_int) // interrupted 4724 emsg(_(e_interr)); 4725 else if (got_match) // did find something but nothing substituted 4726 msg(""); 4727 else if (subflags.do_error) // nothing found 4728 semsg(_(e_patnotf2), get_search_pat()); 4729 } 4730 4731 #ifdef FEAT_FOLDING 4732 if (subflags.do_ask && hasAnyFolding(curwin)) 4733 // Cursor position may require updating 4734 changed_window_setting(); 4735 #endif 4736 4737 vim_regfree(regmatch.regprog); 4738 4739 // Restore the flag values, they can be used for ":&&". 4740 subflags.do_all = save_do_all; 4741 subflags.do_ask = save_do_ask; 4742 } 4743 4744 /* 4745 * Give message for number of substitutions. 4746 * Can also be used after a ":global" command. 4747 * Return TRUE if a message was given. 4748 */ 4749 int 4750 do_sub_msg( 4751 int count_only) // used 'n' flag for ":s" 4752 { 4753 /* 4754 * Only report substitutions when: 4755 * - more than 'report' substitutions 4756 * - command was typed by user, or number of changed lines > 'report' 4757 * - giving messages is not disabled by 'lazyredraw' 4758 */ 4759 if (((sub_nsubs > p_report && (KeyTyped || sub_nlines > 1 || p_report < 1)) 4760 || count_only) 4761 && messaging()) 4762 { 4763 char *msg_single; 4764 char *msg_plural; 4765 4766 if (got_int) 4767 STRCPY(msg_buf, _("(Interrupted) ")); 4768 else 4769 *msg_buf = NUL; 4770 4771 msg_single = count_only 4772 ? NGETTEXT("%ld match on %ld line", 4773 "%ld matches on %ld line", sub_nsubs) 4774 : NGETTEXT("%ld substitution on %ld line", 4775 "%ld substitutions on %ld line", sub_nsubs); 4776 msg_plural = count_only 4777 ? NGETTEXT("%ld match on %ld lines", 4778 "%ld matches on %ld lines", sub_nsubs) 4779 : NGETTEXT("%ld substitution on %ld lines", 4780 "%ld substitutions on %ld lines", sub_nsubs); 4781 4782 vim_snprintf_add(msg_buf, sizeof(msg_buf), 4783 NGETTEXT(msg_single, msg_plural, sub_nlines), 4784 sub_nsubs, (long)sub_nlines); 4785 4786 if (msg(msg_buf)) 4787 // save message to display it after redraw 4788 set_keep_msg((char_u *)msg_buf, 0); 4789 return TRUE; 4790 } 4791 if (got_int) 4792 { 4793 emsg(_(e_interr)); 4794 return TRUE; 4795 } 4796 return FALSE; 4797 } 4798 4799 static void 4800 global_exe_one(char_u *cmd, linenr_T lnum) 4801 { 4802 curwin->w_cursor.lnum = lnum; 4803 curwin->w_cursor.col = 0; 4804 if (*cmd == NUL || *cmd == '\n') 4805 do_cmdline((char_u *)"p", NULL, NULL, DOCMD_NOWAIT); 4806 else 4807 do_cmdline(cmd, NULL, NULL, DOCMD_NOWAIT); 4808 } 4809 4810 /* 4811 * Execute a global command of the form: 4812 * 4813 * g/pattern/X : execute X on all lines where pattern matches 4814 * v/pattern/X : execute X on all lines where pattern does not match 4815 * 4816 * where 'X' is an EX command 4817 * 4818 * The command character (as well as the trailing slash) is optional, and 4819 * is assumed to be 'p' if missing. 4820 * 4821 * This is implemented in two passes: first we scan the file for the pattern and 4822 * set a mark for each line that (not) matches. Secondly we execute the command 4823 * for each line that has a mark. This is required because after deleting 4824 * lines we do not know where to search for the next match. 4825 */ 4826 void 4827 ex_global(exarg_T *eap) 4828 { 4829 linenr_T lnum; // line number according to old situation 4830 int ndone = 0; 4831 int type; // first char of cmd: 'v' or 'g' 4832 char_u *cmd; // command argument 4833 4834 char_u delim; // delimiter, normally '/' 4835 char_u *pat; 4836 regmmatch_T regmatch; 4837 int match; 4838 int which_pat; 4839 4840 // When nesting the command works on one line. This allows for 4841 // ":g/found/v/notfound/command". 4842 if (global_busy && (eap->line1 != 1 4843 || eap->line2 != curbuf->b_ml.ml_line_count)) 4844 { 4845 // will increment global_busy to break out of the loop 4846 emsg(_("E147: Cannot do :global recursive with a range")); 4847 return; 4848 } 4849 4850 if (eap->forceit) // ":global!" is like ":vglobal" 4851 type = 'v'; 4852 else 4853 type = *eap->cmd; 4854 cmd = eap->arg; 4855 which_pat = RE_LAST; // default: use last used regexp 4856 4857 /* 4858 * undocumented vi feature: 4859 * "\/" and "\?": use previous search pattern. 4860 * "\&": use previous substitute pattern. 4861 */ 4862 if (*cmd == '\\') 4863 { 4864 ++cmd; 4865 if (vim_strchr((char_u *)"/?&", *cmd) == NULL) 4866 { 4867 emsg(_(e_backslash)); 4868 return; 4869 } 4870 if (*cmd == '&') 4871 which_pat = RE_SUBST; // use previous substitute pattern 4872 else 4873 which_pat = RE_SEARCH; // use previous search pattern 4874 ++cmd; 4875 pat = (char_u *)""; 4876 } 4877 else if (*cmd == NUL) 4878 { 4879 emsg(_("E148: Regular expression missing from global")); 4880 return; 4881 } 4882 else 4883 { 4884 delim = *cmd; // get the delimiter 4885 if (delim) 4886 ++cmd; // skip delimiter if there is one 4887 pat = cmd; // remember start of pattern 4888 cmd = skip_regexp_ex(cmd, delim, magic_isset(), &eap->arg, NULL, NULL); 4889 if (cmd[0] == delim) // end delimiter found 4890 *cmd++ = NUL; // replace it with a NUL 4891 } 4892 4893 if (search_regcomp(pat, RE_BOTH, which_pat, SEARCH_HIS, ®match) == FAIL) 4894 { 4895 emsg(_(e_invalid_command)); 4896 return; 4897 } 4898 4899 if (global_busy) 4900 { 4901 lnum = curwin->w_cursor.lnum; 4902 match = vim_regexec_multi(®match, curwin, curbuf, lnum, 4903 (colnr_T)0, NULL, NULL); 4904 if ((type == 'g' && match) || (type == 'v' && !match)) 4905 global_exe_one(cmd, lnum); 4906 } 4907 else 4908 { 4909 /* 4910 * pass 1: set marks for each (not) matching line 4911 */ 4912 for (lnum = eap->line1; lnum <= eap->line2 && !got_int; ++lnum) 4913 { 4914 // a match on this line? 4915 match = vim_regexec_multi(®match, curwin, curbuf, lnum, 4916 (colnr_T)0, NULL, NULL); 4917 if ((type == 'g' && match) || (type == 'v' && !match)) 4918 { 4919 ml_setmarked(lnum); 4920 ndone++; 4921 } 4922 line_breakcheck(); 4923 } 4924 4925 /* 4926 * pass 2: execute the command for each line that has been marked 4927 */ 4928 if (got_int) 4929 msg(_(e_interr)); 4930 else if (ndone == 0) 4931 { 4932 if (type == 'v') 4933 smsg(_("Pattern found in every line: %s"), pat); 4934 else 4935 smsg(_("Pattern not found: %s"), pat); 4936 } 4937 else 4938 { 4939 #ifdef FEAT_CLIPBOARD 4940 start_global_changes(); 4941 #endif 4942 global_exe(cmd); 4943 #ifdef FEAT_CLIPBOARD 4944 end_global_changes(); 4945 #endif 4946 } 4947 4948 ml_clearmarked(); // clear rest of the marks 4949 } 4950 4951 vim_regfree(regmatch.regprog); 4952 } 4953 4954 /* 4955 * Execute "cmd" on lines marked with ml_setmarked(). 4956 */ 4957 void 4958 global_exe(char_u *cmd) 4959 { 4960 linenr_T old_lcount; // b_ml.ml_line_count before the command 4961 buf_T *old_buf = curbuf; // remember what buffer we started in 4962 linenr_T lnum; // line number according to old situation 4963 4964 /* 4965 * Set current position only once for a global command. 4966 * If global_busy is set, setpcmark() will not do anything. 4967 * If there is an error, global_busy will be incremented. 4968 */ 4969 setpcmark(); 4970 4971 // When the command writes a message, don't overwrite the command. 4972 msg_didout = TRUE; 4973 4974 sub_nsubs = 0; 4975 sub_nlines = 0; 4976 global_need_beginline = FALSE; 4977 global_busy = 1; 4978 old_lcount = curbuf->b_ml.ml_line_count; 4979 while (!got_int && (lnum = ml_firstmarked()) != 0 && global_busy == 1) 4980 { 4981 global_exe_one(cmd, lnum); 4982 ui_breakcheck(); 4983 } 4984 4985 global_busy = 0; 4986 if (global_need_beginline) 4987 beginline(BL_WHITE | BL_FIX); 4988 else 4989 check_cursor(); // cursor may be beyond the end of the line 4990 4991 // the cursor may not have moved in the text but a change in a previous 4992 // line may move it on the screen 4993 changed_line_abv_curs(); 4994 4995 // If it looks like no message was written, allow overwriting the 4996 // command with the report for number of changes. 4997 if (msg_col == 0 && msg_scrolled == 0) 4998 msg_didout = FALSE; 4999 5000 // If substitutes done, report number of substitutes, otherwise report 5001 // number of extra or deleted lines. 5002 // Don't report extra or deleted lines in the edge case where the buffer 5003 // we are in after execution is different from the buffer we started in. 5004 if (!do_sub_msg(FALSE) && curbuf == old_buf) 5005 msgmore(curbuf->b_ml.ml_line_count - old_lcount); 5006 } 5007 5008 #ifdef FEAT_VIMINFO 5009 /* 5010 * Get the previous substitute pattern. 5011 */ 5012 char_u * 5013 get_old_sub(void) 5014 { 5015 return old_sub; 5016 } 5017 5018 /* 5019 * Set the previous substitute pattern. "val" must be allocated. 5020 */ 5021 void 5022 set_old_sub(char_u *val) 5023 { 5024 vim_free(old_sub); 5025 old_sub = val; 5026 } 5027 #endif // FEAT_VIMINFO 5028 5029 #if defined(EXITFREE) || defined(PROTO) 5030 void 5031 free_old_sub(void) 5032 { 5033 vim_free(old_sub); 5034 } 5035 #endif 5036 5037 #if defined(FEAT_QUICKFIX) || defined(PROTO) 5038 /* 5039 * Set up for a tagpreview. 5040 * Makes the preview window the current window. 5041 * Return TRUE when it was created. 5042 */ 5043 int 5044 prepare_tagpreview( 5045 int undo_sync, // sync undo when leaving the window 5046 int use_previewpopup, // use popup if 'previewpopup' set 5047 use_popup_T use_popup) // use other popup window 5048 { 5049 win_T *wp; 5050 5051 # ifdef FEAT_GUI 5052 need_mouse_correct = TRUE; 5053 # endif 5054 5055 /* 5056 * If there is already a preview window open, use that one. 5057 */ 5058 if (!curwin->w_p_pvw) 5059 { 5060 # ifdef FEAT_PROP_POPUP 5061 if (use_previewpopup && *p_pvp != NUL) 5062 { 5063 wp = popup_find_preview_window(); 5064 if (wp != NULL) 5065 popup_set_wantpos_cursor(wp, wp->w_minwidth, NULL); 5066 } 5067 else if (use_popup != USEPOPUP_NONE) 5068 { 5069 wp = popup_find_info_window(); 5070 if (wp != NULL) 5071 { 5072 if (use_popup == USEPOPUP_NORMAL) 5073 popup_show(wp); 5074 else 5075 popup_hide(wp); 5076 // When the popup moves or resizes it may reveal part of 5077 // another window. TODO: can this be done more efficiently? 5078 redraw_all_later(NOT_VALID); 5079 } 5080 } 5081 else 5082 # endif 5083 { 5084 FOR_ALL_WINDOWS(wp) 5085 if (wp->w_p_pvw) 5086 break; 5087 } 5088 if (wp != NULL) 5089 win_enter(wp, undo_sync); 5090 else 5091 { 5092 /* 5093 * There is no preview window open yet. Create one. 5094 */ 5095 # ifdef FEAT_PROP_POPUP 5096 if ((use_previewpopup && *p_pvp != NUL) 5097 || use_popup != USEPOPUP_NONE) 5098 return popup_create_preview_window(use_popup != USEPOPUP_NONE); 5099 # endif 5100 if (win_split(g_do_tagpreview > 0 ? g_do_tagpreview : 0, 0) == FAIL) 5101 return FALSE; 5102 curwin->w_p_pvw = TRUE; 5103 curwin->w_p_wfh = TRUE; 5104 RESET_BINDING(curwin); // don't take over 'scrollbind' 5105 // and 'cursorbind' 5106 # ifdef FEAT_DIFF 5107 curwin->w_p_diff = FALSE; // no 'diff' 5108 # endif 5109 # ifdef FEAT_FOLDING 5110 curwin->w_p_fdc = 0; // no 'foldcolumn' 5111 # endif 5112 return TRUE; 5113 } 5114 } 5115 return FALSE; 5116 } 5117 5118 #endif 5119 5120 /* 5121 * Make the user happy. 5122 */ 5123 void 5124 ex_smile(exarg_T *eap UNUSED) 5125 { 5126 static char *code[] = { 5127 "\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$", 5128 "\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" 5129 }; 5130 char *p; 5131 int n; 5132 int i; 5133 5134 msg_start(); 5135 msg_putchar('\n'); 5136 for (i = 0; i < 2; ++i) 5137 for (p = code[i]; *p != NUL; ++p) 5138 if (*p == 'x') 5139 msg_putchar('\n'); 5140 else 5141 for (n = *p++; n > 0; --n) 5142 if (*p == 'o' || *p == '$') 5143 msg_putchar_attr(*p, HL_ATTR(HLF_L)); 5144 else 5145 msg_putchar(*p); 5146 msg_clr_eos(); 5147 } 5148 5149 /* 5150 * ":drop" 5151 * Opens the first argument in a window. When there are two or more arguments 5152 * the argument list is redefined. 5153 */ 5154 void 5155 ex_drop(exarg_T *eap) 5156 { 5157 int split = FALSE; 5158 win_T *wp; 5159 buf_T *buf; 5160 tabpage_T *tp; 5161 5162 if (ERROR_IF_POPUP_WINDOW || ERROR_IF_TERM_POPUP_WINDOW) 5163 return; 5164 5165 /* 5166 * Check if the first argument is already being edited in a window. If 5167 * so, jump to that window. 5168 * We would actually need to check all arguments, but that's complicated 5169 * and mostly only one file is dropped. 5170 * This also ignores wildcards, since it is very unlikely the user is 5171 * editing a file name with a wildcard character. 5172 */ 5173 set_arglist(eap->arg); 5174 5175 /* 5176 * Expanding wildcards may result in an empty argument list. E.g. when 5177 * editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we 5178 * already did an error message for this. 5179 */ 5180 if (ARGCOUNT == 0) 5181 return; 5182 5183 if (cmdmod.cmod_tab) 5184 { 5185 // ":tab drop file ...": open a tab for each argument that isn't 5186 // edited in a window yet. It's like ":tab all" but without closing 5187 // windows or tabs. 5188 ex_all(eap); 5189 } 5190 else 5191 { 5192 // ":drop file ...": Edit the first argument. Jump to an existing 5193 // window if possible, edit in current window if the current buffer 5194 // can be abandoned, otherwise open a new window. 5195 buf = buflist_findnr(ARGLIST[0].ae_fnum); 5196 5197 FOR_ALL_TAB_WINDOWS(tp, wp) 5198 { 5199 if (wp->w_buffer == buf) 5200 { 5201 goto_tabpage_win(tp, wp); 5202 curwin->w_arg_idx = 0; 5203 if (!bufIsChanged(curbuf)) 5204 { 5205 int save_ar = curbuf->b_p_ar; 5206 5207 // reload the file if it is newer 5208 curbuf->b_p_ar = TRUE; 5209 buf_check_timestamp(curbuf, FALSE); 5210 curbuf->b_p_ar = save_ar; 5211 } 5212 return; 5213 } 5214 } 5215 5216 /* 5217 * Check whether the current buffer is changed. If so, we will need 5218 * to split the current window or data could be lost. 5219 * Skip the check if the 'hidden' option is set, as in this case the 5220 * buffer won't be lost. 5221 */ 5222 if (!buf_hide(curbuf)) 5223 { 5224 ++emsg_off; 5225 split = check_changed(curbuf, CCGD_AW | CCGD_EXCMD); 5226 --emsg_off; 5227 } 5228 5229 // Fake a ":sfirst" or ":first" command edit the first argument. 5230 if (split) 5231 { 5232 eap->cmdidx = CMD_sfirst; 5233 eap->cmd[0] = 's'; 5234 } 5235 else 5236 eap->cmdidx = CMD_first; 5237 ex_rewind(eap); 5238 } 5239 } 5240 5241 /* 5242 * Skip over the pattern argument of ":vimgrep /pat/[g][j]". 5243 * Put the start of the pattern in "*s", unless "s" is NULL. 5244 * If "flags" is not NULL put the flags in it: VGR_GLOBAL, VGR_NOJUMP. 5245 * If "s" is not NULL terminate the pattern with a NUL. 5246 * Return a pointer to the char just past the pattern plus flags. 5247 */ 5248 char_u * 5249 skip_vimgrep_pat(char_u *p, char_u **s, int *flags) 5250 { 5251 int c; 5252 5253 if (vim_isIDc(*p)) 5254 { 5255 // ":vimgrep pattern fname" 5256 if (s != NULL) 5257 *s = p; 5258 p = skiptowhite(p); 5259 if (s != NULL && *p != NUL) 5260 *p++ = NUL; 5261 } 5262 else 5263 { 5264 // ":vimgrep /pattern/[g][j] fname" 5265 if (s != NULL) 5266 *s = p + 1; 5267 c = *p; 5268 p = skip_regexp(p + 1, c, TRUE); 5269 if (*p != c) 5270 return NULL; 5271 5272 // Truncate the pattern. 5273 if (s != NULL) 5274 *p = NUL; 5275 ++p; 5276 5277 // Find the flags 5278 while (*p == 'g' || *p == 'j') 5279 { 5280 if (flags != NULL) 5281 { 5282 if (*p == 'g') 5283 *flags |= VGR_GLOBAL; 5284 else 5285 *flags |= VGR_NOJUMP; 5286 } 5287 ++p; 5288 } 5289 } 5290 return p; 5291 } 5292 5293 #if defined(FEAT_EVAL) || defined(PROTO) 5294 /* 5295 * List v:oldfiles in a nice way. 5296 */ 5297 void 5298 ex_oldfiles(exarg_T *eap UNUSED) 5299 { 5300 list_T *l = get_vim_var_list(VV_OLDFILES); 5301 listitem_T *li; 5302 int nr = 0; 5303 char_u *fname; 5304 5305 if (l == NULL) 5306 msg(_("No old files")); 5307 else 5308 { 5309 msg_start(); 5310 msg_scroll = TRUE; 5311 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next) 5312 { 5313 ++nr; 5314 fname = tv_get_string(&li->li_tv); 5315 if (!message_filtered(fname)) 5316 { 5317 msg_outnum((long)nr); 5318 msg_puts(": "); 5319 msg_outtrans(fname); 5320 msg_clr_eos(); 5321 msg_putchar('\n'); 5322 out_flush(); // output one line at a time 5323 ui_breakcheck(); 5324 } 5325 } 5326 5327 // Assume "got_int" was set to truncate the listing. 5328 got_int = FALSE; 5329 5330 # ifdef FEAT_BROWSE_CMD 5331 if (cmdmod.cmod_flags & CMOD_BROWSE) 5332 { 5333 quit_more = FALSE; 5334 nr = prompt_for_number(FALSE); 5335 msg_starthere(); 5336 if (nr > 0) 5337 { 5338 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES), 5339 (long)nr); 5340 5341 if (p != NULL) 5342 { 5343 p = expand_env_save(p); 5344 eap->arg = p; 5345 eap->cmdidx = CMD_edit; 5346 cmdmod.cmod_flags &= ~CMOD_BROWSE; 5347 do_exedit(eap, NULL); 5348 vim_free(p); 5349 } 5350 } 5351 } 5352 # endif 5353 } 5354 } 5355 #endif 5356