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 * ops.c: implementation of various operators: op_shift, op_delete, op_tilde, 12 * op_change, op_yank, do_put, do_join 13 */ 14 15 #include "vim.h" 16 17 /* 18 * Number of registers. 19 * 0 = unnamed register, for normal yanks and puts 20 * 1..9 = registers '1' to '9', for deletes 21 * 10..35 = registers 'a' to 'z' 22 * 36 = delete register '-' 23 * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined 24 * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined 25 */ 26 /* 27 * Symbolic names for some registers. 28 */ 29 #define DELETION_REGISTER 36 30 #ifdef FEAT_CLIPBOARD 31 # define STAR_REGISTER 37 32 # ifdef FEAT_X11 33 # define PLUS_REGISTER 38 34 # else 35 # define PLUS_REGISTER STAR_REGISTER /* there is only one */ 36 # endif 37 #endif 38 #ifdef FEAT_DND 39 # define TILDE_REGISTER (PLUS_REGISTER + 1) 40 #endif 41 42 #ifdef FEAT_CLIPBOARD 43 # ifdef FEAT_DND 44 # define NUM_REGISTERS (TILDE_REGISTER + 1) 45 # else 46 # define NUM_REGISTERS (PLUS_REGISTER + 1) 47 # endif 48 #else 49 # define NUM_REGISTERS 37 50 #endif 51 52 /* 53 * Each yank register has an array of pointers to lines. 54 */ 55 typedef struct 56 { 57 char_u **y_array; /* pointer to array of line pointers */ 58 linenr_T y_size; /* number of lines in y_array */ 59 char_u y_type; /* MLINE, MCHAR or MBLOCK */ 60 colnr_T y_width; /* only set if y_type == MBLOCK */ 61 #ifdef FEAT_VIMINFO 62 time_t y_time_set; 63 #endif 64 } yankreg_T; 65 66 static yankreg_T y_regs[NUM_REGISTERS]; 67 68 static yankreg_T *y_current; /* ptr to current yankreg */ 69 static int y_append; /* TRUE when appending */ 70 static yankreg_T *y_previous = NULL; /* ptr to last written yankreg */ 71 72 /* 73 * structure used by block_prep, op_delete and op_yank for blockwise operators 74 * also op_change, op_shift, op_insert, op_replace - AKelly 75 */ 76 struct block_def 77 { 78 int startspaces; /* 'extra' cols before first char */ 79 int endspaces; /* 'extra' cols after last char */ 80 int textlen; /* chars in block */ 81 char_u *textstart; /* pointer to 1st char (partially) in block */ 82 colnr_T textcol; /* index of chars (partially) in block */ 83 colnr_T start_vcol; /* start col of 1st char wholly inside block */ 84 colnr_T end_vcol; /* start col of 1st char wholly after block */ 85 #ifdef FEAT_VISUALEXTRA 86 int is_short; /* TRUE if line is too short to fit in block */ 87 int is_MAX; /* TRUE if curswant==MAXCOL when starting */ 88 int is_oneChar; /* TRUE if block within one character */ 89 int pre_whitesp; /* screen cols of ws before block */ 90 int pre_whitesp_c; /* chars of ws before block */ 91 colnr_T end_char_vcols; /* number of vcols of post-block char */ 92 #endif 93 colnr_T start_char_vcols; /* number of vcols of pre-block char */ 94 }; 95 96 #ifdef FEAT_VISUALEXTRA 97 static void shift_block(oparg_T *oap, int amount); 98 static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def*bdp); 99 #endif 100 static int stuff_yank(int, char_u *); 101 static void put_reedit_in_typebuf(int silent); 102 static int put_in_typebuf(char_u *s, int esc, int colon, 103 int silent); 104 static void stuffescaped(char_u *arg, int literally); 105 #ifdef FEAT_MBYTE 106 static void mb_adjust_opend(oparg_T *oap); 107 #endif 108 static void free_yank(long); 109 static void free_yank_all(void); 110 static int yank_copy_line(struct block_def *bd, long y_idx); 111 #ifdef FEAT_CLIPBOARD 112 static void copy_yank_reg(yankreg_T *reg); 113 static void may_set_selection(void); 114 #endif 115 static void dis_msg(char_u *p, int skip_esc); 116 static void block_prep(oparg_T *oap, struct block_def *, linenr_T, int); 117 static int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1); 118 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) 119 static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list); 120 #endif 121 static int ends_in_white(linenr_T lnum); 122 #ifdef FEAT_COMMENTS 123 static int same_leader(linenr_T lnum, int, char_u *, int, char_u *); 124 static int fmt_check_par(linenr_T, int *, char_u **, int do_comments); 125 #else 126 static int fmt_check_par(linenr_T); 127 #endif 128 129 // Flags for third item in "opchars". 130 #define OPF_LINES 1 // operator always works on lines 131 #define OPF_CHANGE 2 // operator changes text 132 133 /* 134 * The names of operators. 135 * IMPORTANT: Index must correspond with defines in vim.h!!! 136 * The third field holds OPF_ flags. 137 */ 138 static char opchars[][3] = 139 { 140 {NUL, NUL, 0}, // OP_NOP 141 {'d', NUL, OPF_CHANGE}, // OP_DELETE 142 {'y', NUL, 0}, // OP_YANK 143 {'c', NUL, OPF_CHANGE}, // OP_CHANGE 144 {'<', NUL, OPF_LINES | OPF_CHANGE}, // OP_LSHIFT 145 {'>', NUL, OPF_LINES | OPF_CHANGE}, // OP_RSHIFT 146 {'!', NUL, OPF_LINES | OPF_CHANGE}, // OP_FILTER 147 {'g', '~', OPF_CHANGE}, // OP_TILDE 148 {'=', NUL, OPF_LINES | OPF_CHANGE}, // OP_INDENT 149 {'g', 'q', OPF_LINES | OPF_CHANGE}, // OP_FORMAT 150 {':', NUL, OPF_LINES}, // OP_COLON 151 {'g', 'U', OPF_CHANGE}, // OP_UPPER 152 {'g', 'u', OPF_CHANGE}, // OP_LOWER 153 {'J', NUL, OPF_LINES | OPF_CHANGE}, // DO_JOIN 154 {'g', 'J', OPF_LINES | OPF_CHANGE}, // DO_JOIN_NS 155 {'g', '?', OPF_CHANGE}, // OP_ROT13 156 {'r', NUL, OPF_CHANGE}, // OP_REPLACE 157 {'I', NUL, OPF_CHANGE}, // OP_INSERT 158 {'A', NUL, OPF_CHANGE}, // OP_APPEND 159 {'z', 'f', OPF_LINES}, // OP_FOLD 160 {'z', 'o', OPF_LINES}, // OP_FOLDOPEN 161 {'z', 'O', OPF_LINES}, // OP_FOLDOPENREC 162 {'z', 'c', OPF_LINES}, // OP_FOLDCLOSE 163 {'z', 'C', OPF_LINES}, // OP_FOLDCLOSEREC 164 {'z', 'd', OPF_LINES}, // OP_FOLDDEL 165 {'z', 'D', OPF_LINES}, // OP_FOLDDELREC 166 {'g', 'w', OPF_LINES | OPF_CHANGE}, // OP_FORMAT2 167 {'g', '@', OPF_CHANGE}, // OP_FUNCTION 168 {Ctrl_A, NUL, OPF_CHANGE}, // OP_NR_ADD 169 {Ctrl_X, NUL, OPF_CHANGE}, // OP_NR_SUB 170 }; 171 172 /* 173 * Translate a command name into an operator type. 174 * Must only be called with a valid operator name! 175 */ 176 int 177 get_op_type(int char1, int char2) 178 { 179 int i; 180 181 if (char1 == 'r') /* ignore second character */ 182 return OP_REPLACE; 183 if (char1 == '~') /* when tilde is an operator */ 184 return OP_TILDE; 185 if (char1 == 'g' && char2 == Ctrl_A) /* add */ 186 return OP_NR_ADD; 187 if (char1 == 'g' && char2 == Ctrl_X) /* subtract */ 188 return OP_NR_SUB; 189 for (i = 0; ; ++i) 190 { 191 if (opchars[i][0] == char1 && opchars[i][1] == char2) 192 break; 193 if (i == (int)(sizeof(opchars) / sizeof(char [3]) - 1)) 194 { 195 internal_error("get_op_type()"); 196 break; 197 } 198 } 199 return i; 200 } 201 202 /* 203 * Return TRUE if operator "op" always works on whole lines. 204 */ 205 int 206 op_on_lines(int op) 207 { 208 return opchars[op][2] & OPF_LINES; 209 } 210 211 /* 212 * Return TRUE if operator "op" changes text. 213 */ 214 int 215 op_is_change(int op) 216 { 217 return opchars[op][2] & OPF_CHANGE; 218 } 219 220 /* 221 * Get first operator command character. 222 * Returns 'g' or 'z' if there is another command character. 223 */ 224 int 225 get_op_char(int optype) 226 { 227 return opchars[optype][0]; 228 } 229 230 /* 231 * Get second operator command character. 232 */ 233 int 234 get_extra_op_char(int optype) 235 { 236 return opchars[optype][1]; 237 } 238 239 /* 240 * op_shift - handle a shift operation 241 */ 242 void 243 op_shift(oparg_T *oap, int curs_top, int amount) 244 { 245 long i; 246 int first_char; 247 char_u *s; 248 int block_col = 0; 249 250 if (u_save((linenr_T)(oap->start.lnum - 1), 251 (linenr_T)(oap->end.lnum + 1)) == FAIL) 252 return; 253 254 if (oap->block_mode) 255 block_col = curwin->w_cursor.col; 256 257 for (i = oap->line_count; --i >= 0; ) 258 { 259 first_char = *ml_get_curline(); 260 if (first_char == NUL) /* empty line */ 261 curwin->w_cursor.col = 0; 262 #ifdef FEAT_VISUALEXTRA 263 else if (oap->block_mode) 264 shift_block(oap, amount); 265 #endif 266 else 267 /* Move the line right if it doesn't start with '#', 'smartindent' 268 * isn't set or 'cindent' isn't set or '#' isn't in 'cino'. */ 269 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) 270 if (first_char != '#' || !preprocs_left()) 271 #endif 272 { 273 shift_line(oap->op_type == OP_LSHIFT, p_sr, amount, FALSE); 274 } 275 ++curwin->w_cursor.lnum; 276 } 277 278 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); 279 if (oap->block_mode) 280 { 281 curwin->w_cursor.lnum = oap->start.lnum; 282 curwin->w_cursor.col = block_col; 283 } 284 else if (curs_top) /* put cursor on first line, for ">>" */ 285 { 286 curwin->w_cursor.lnum = oap->start.lnum; 287 beginline(BL_SOL | BL_FIX); /* shift_line() may have set cursor.col */ 288 } 289 else 290 --curwin->w_cursor.lnum; /* put cursor on last line, for ":>" */ 291 292 #ifdef FEAT_FOLDING 293 /* The cursor line is not in a closed fold */ 294 foldOpenCursor(); 295 #endif 296 297 298 if (oap->line_count > p_report) 299 { 300 if (oap->op_type == OP_RSHIFT) 301 s = (char_u *)">"; 302 else 303 s = (char_u *)"<"; 304 if (oap->line_count == 1) 305 { 306 if (amount == 1) 307 sprintf((char *)IObuff, _("1 line %sed 1 time"), s); 308 else 309 sprintf((char *)IObuff, _("1 line %sed %d times"), s, amount); 310 } 311 else 312 { 313 if (amount == 1) 314 sprintf((char *)IObuff, _("%ld lines %sed 1 time"), 315 oap->line_count, s); 316 else 317 sprintf((char *)IObuff, _("%ld lines %sed %d times"), 318 oap->line_count, s, amount); 319 } 320 msg(IObuff); 321 } 322 323 /* 324 * Set "'[" and "']" marks. 325 */ 326 curbuf->b_op_start = oap->start; 327 curbuf->b_op_end.lnum = oap->end.lnum; 328 curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); 329 if (curbuf->b_op_end.col > 0) 330 --curbuf->b_op_end.col; 331 } 332 333 /* 334 * shift the current line one shiftwidth left (if left != 0) or right 335 * leaves cursor on first blank in the line 336 */ 337 void 338 shift_line( 339 int left, 340 int round, 341 int amount, 342 int call_changed_bytes) /* call changed_bytes() */ 343 { 344 int count; 345 int i, j; 346 int p_sw = (int)get_sw_value(curbuf); 347 348 count = get_indent(); /* get current indent */ 349 350 if (round) /* round off indent */ 351 { 352 i = count / p_sw; /* number of p_sw rounded down */ 353 j = count % p_sw; /* extra spaces */ 354 if (j && left) /* first remove extra spaces */ 355 --amount; 356 if (left) 357 { 358 i -= amount; 359 if (i < 0) 360 i = 0; 361 } 362 else 363 i += amount; 364 count = i * p_sw; 365 } 366 else /* original vi indent */ 367 { 368 if (left) 369 { 370 count -= p_sw * amount; 371 if (count < 0) 372 count = 0; 373 } 374 else 375 count += p_sw * amount; 376 } 377 378 /* Set new indent */ 379 #ifdef FEAT_VREPLACE 380 if (State & VREPLACE_FLAG) 381 change_indent(INDENT_SET, count, FALSE, NUL, call_changed_bytes); 382 else 383 #endif 384 (void)set_indent(count, call_changed_bytes ? SIN_CHANGED : 0); 385 } 386 387 #if defined(FEAT_VISUALEXTRA) || defined(PROTO) 388 /* 389 * Shift one line of the current block one shiftwidth right or left. 390 * Leaves cursor on first character in block. 391 */ 392 static void 393 shift_block(oparg_T *oap, int amount) 394 { 395 int left = (oap->op_type == OP_LSHIFT); 396 int oldstate = State; 397 int total; 398 char_u *newp, *oldp; 399 int oldcol = curwin->w_cursor.col; 400 int p_sw = (int)get_sw_value(curbuf); 401 #ifdef FEAT_VARTABS 402 int *p_vts = curbuf->b_p_vts_array; 403 #endif 404 int p_ts = (int)curbuf->b_p_ts; 405 struct block_def bd; 406 int incr; 407 colnr_T ws_vcol; 408 int i = 0, j = 0; 409 int len; 410 #ifdef FEAT_RIGHTLEFT 411 int old_p_ri = p_ri; 412 413 p_ri = 0; /* don't want revins in indent */ 414 #endif 415 416 State = INSERT; /* don't want REPLACE for State */ 417 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); 418 if (bd.is_short) 419 return; 420 421 /* total is number of screen columns to be inserted/removed */ 422 total = (int)((unsigned)amount * (unsigned)p_sw); 423 if ((total / p_sw) != amount) 424 return; /* multiplication overflow */ 425 426 oldp = ml_get_curline(); 427 428 if (!left) 429 { 430 /* 431 * 1. Get start vcol 432 * 2. Total ws vcols 433 * 3. Divvy into TABs & spp 434 * 4. Construct new string 435 */ 436 total += bd.pre_whitesp; /* all virtual WS upto & incl a split TAB */ 437 ws_vcol = bd.start_vcol - bd.pre_whitesp; 438 if (bd.startspaces) 439 { 440 #ifdef FEAT_MBYTE 441 if (has_mbyte) 442 { 443 if ((*mb_ptr2len)(bd.textstart) == 1) 444 ++bd.textstart; 445 else 446 { 447 ws_vcol = 0; 448 bd.startspaces = 0; 449 } 450 } 451 else 452 #endif 453 ++bd.textstart; 454 } 455 for ( ; VIM_ISWHITE(*bd.textstart); ) 456 { 457 /* TODO: is passing bd.textstart for start of the line OK? */ 458 incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, 459 (colnr_T)(bd.start_vcol)); 460 total += incr; 461 bd.start_vcol += incr; 462 } 463 /* OK, now total=all the VWS reqd, and textstart points at the 1st 464 * non-ws char in the block. */ 465 #ifdef FEAT_VARTABS 466 if (!curbuf->b_p_et) 467 tabstop_fromto(ws_vcol, ws_vcol + total, p_ts, p_vts, &i, &j); 468 else 469 j = total; 470 #else 471 if (!curbuf->b_p_et) 472 i = ((ws_vcol % p_ts) + total) / p_ts; /* number of tabs */ 473 if (i) 474 j = ((ws_vcol % p_ts) + total) % p_ts; /* number of spp */ 475 else 476 j = total; 477 #endif 478 /* if we're splitting a TAB, allow for it */ 479 bd.textcol -= bd.pre_whitesp_c - (bd.startspaces != 0); 480 len = (int)STRLEN(bd.textstart) + 1; 481 newp = alloc_check((unsigned)(bd.textcol + i + j + len)); 482 if (newp == NULL) 483 return; 484 vim_memset(newp, NUL, (size_t)(bd.textcol + i + j + len)); 485 mch_memmove(newp, oldp, (size_t)bd.textcol); 486 vim_memset(newp + bd.textcol, TAB, (size_t)i); 487 vim_memset(newp + bd.textcol + i, ' ', (size_t)j); 488 /* the end */ 489 mch_memmove(newp + bd.textcol + i + j, bd.textstart, (size_t)len); 490 } 491 else /* left */ 492 { 493 colnr_T destination_col; /* column to which text in block will 494 be shifted */ 495 char_u *verbatim_copy_end; /* end of the part of the line which is 496 copied verbatim */ 497 colnr_T verbatim_copy_width;/* the (displayed) width of this part 498 of line */ 499 unsigned fill; /* nr of spaces that replace a TAB */ 500 unsigned new_line_len; /* the length of the line after the 501 block shift */ 502 size_t block_space_width; 503 size_t shift_amount; 504 char_u *non_white = bd.textstart; 505 colnr_T non_white_col; 506 507 /* 508 * Firstly, let's find the first non-whitespace character that is 509 * displayed after the block's start column and the character's column 510 * number. Also, let's calculate the width of all the whitespace 511 * characters that are displayed in the block and precede the searched 512 * non-whitespace character. 513 */ 514 515 /* If "bd.startspaces" is set, "bd.textstart" points to the character, 516 * the part of which is displayed at the block's beginning. Let's start 517 * searching from the next character. */ 518 if (bd.startspaces) 519 MB_PTR_ADV(non_white); 520 521 /* The character's column is in "bd.start_vcol". */ 522 non_white_col = bd.start_vcol; 523 524 while (VIM_ISWHITE(*non_white)) 525 { 526 incr = lbr_chartabsize_adv(bd.textstart, &non_white, non_white_col); 527 non_white_col += incr; 528 } 529 530 block_space_width = non_white_col - oap->start_vcol; 531 /* We will shift by "total" or "block_space_width", whichever is less. 532 */ 533 shift_amount = (block_space_width < (size_t)total 534 ? block_space_width : (size_t)total); 535 536 /* The column to which we will shift the text. */ 537 destination_col = (colnr_T)(non_white_col - shift_amount); 538 539 /* Now let's find out how much of the beginning of the line we can 540 * reuse without modification. */ 541 verbatim_copy_end = bd.textstart; 542 verbatim_copy_width = bd.start_vcol; 543 544 /* If "bd.startspaces" is set, "bd.textstart" points to the character 545 * preceding the block. We have to subtract its width to obtain its 546 * column number. */ 547 if (bd.startspaces) 548 verbatim_copy_width -= bd.start_char_vcols; 549 while (verbatim_copy_width < destination_col) 550 { 551 char_u *line = verbatim_copy_end; 552 553 /* TODO: is passing verbatim_copy_end for start of the line OK? */ 554 incr = lbr_chartabsize(line, verbatim_copy_end, 555 verbatim_copy_width); 556 if (verbatim_copy_width + incr > destination_col) 557 break; 558 verbatim_copy_width += incr; 559 MB_PTR_ADV(verbatim_copy_end); 560 } 561 562 /* If "destination_col" is different from the width of the initial 563 * part of the line that will be copied, it means we encountered a tab 564 * character, which we will have to partly replace with spaces. */ 565 fill = destination_col - verbatim_copy_width; 566 567 /* The replacement line will consist of: 568 * - the beginning of the original line up to "verbatim_copy_end", 569 * - "fill" number of spaces, 570 * - the rest of the line, pointed to by non_white. */ 571 new_line_len = (unsigned)(verbatim_copy_end - oldp) 572 + fill 573 + (unsigned)STRLEN(non_white) + 1; 574 575 newp = alloc_check(new_line_len); 576 if (newp == NULL) 577 return; 578 mch_memmove(newp, oldp, (size_t)(verbatim_copy_end - oldp)); 579 vim_memset(newp + (verbatim_copy_end - oldp), ' ', (size_t)fill); 580 STRMOVE(newp + (verbatim_copy_end - oldp) + fill, non_white); 581 } 582 /* replace the line */ 583 ml_replace(curwin->w_cursor.lnum, newp, FALSE); 584 changed_bytes(curwin->w_cursor.lnum, (colnr_T)bd.textcol); 585 State = oldstate; 586 curwin->w_cursor.col = oldcol; 587 #ifdef FEAT_RIGHTLEFT 588 p_ri = old_p_ri; 589 #endif 590 } 591 #endif 592 593 #ifdef FEAT_VISUALEXTRA 594 /* 595 * Insert string "s" (b_insert ? before : after) block :AKelly 596 * Caller must prepare for undo. 597 */ 598 static void 599 block_insert( 600 oparg_T *oap, 601 char_u *s, 602 int b_insert, 603 struct block_def *bdp) 604 { 605 int p_ts; 606 int count = 0; /* extra spaces to replace a cut TAB */ 607 int spaces = 0; /* non-zero if cutting a TAB */ 608 colnr_T offset; /* pointer along new line */ 609 unsigned s_len; /* STRLEN(s) */ 610 char_u *newp, *oldp; /* new, old lines */ 611 linenr_T lnum; /* loop var */ 612 int oldstate = State; 613 614 State = INSERT; /* don't want REPLACE for State */ 615 s_len = (unsigned)STRLEN(s); 616 617 for (lnum = oap->start.lnum + 1; lnum <= oap->end.lnum; lnum++) 618 { 619 block_prep(oap, bdp, lnum, TRUE); 620 if (bdp->is_short && b_insert) 621 continue; /* OP_INSERT, line ends before block start */ 622 623 oldp = ml_get(lnum); 624 625 if (b_insert) 626 { 627 p_ts = bdp->start_char_vcols; 628 spaces = bdp->startspaces; 629 if (spaces != 0) 630 count = p_ts - 1; /* we're cutting a TAB */ 631 offset = bdp->textcol; 632 } 633 else /* append */ 634 { 635 p_ts = bdp->end_char_vcols; 636 if (!bdp->is_short) /* spaces = padding after block */ 637 { 638 spaces = (bdp->endspaces ? p_ts - bdp->endspaces : 0); 639 if (spaces != 0) 640 count = p_ts - 1; /* we're cutting a TAB */ 641 offset = bdp->textcol + bdp->textlen - (spaces != 0); 642 } 643 else /* spaces = padding to block edge */ 644 { 645 /* if $ used, just append to EOL (ie spaces==0) */ 646 if (!bdp->is_MAX) 647 spaces = (oap->end_vcol - bdp->end_vcol) + 1; 648 count = spaces; 649 offset = bdp->textcol + bdp->textlen; 650 } 651 } 652 653 #ifdef FEAT_MBYTE 654 if (has_mbyte && spaces > 0) 655 { 656 int off; 657 658 /* Avoid starting halfway a multi-byte character. */ 659 if (b_insert) 660 { 661 off = (*mb_head_off)(oldp, oldp + offset + spaces); 662 } 663 else 664 { 665 off = (*mb_off_next)(oldp, oldp + offset); 666 offset += off; 667 } 668 spaces -= off; 669 count -= off; 670 } 671 #endif 672 673 newp = alloc_check((unsigned)(STRLEN(oldp)) + s_len + count + 1); 674 if (newp == NULL) 675 continue; 676 677 /* copy up to shifted part */ 678 mch_memmove(newp, oldp, (size_t)(offset)); 679 oldp += offset; 680 681 /* insert pre-padding */ 682 vim_memset(newp + offset, ' ', (size_t)spaces); 683 684 /* copy the new text */ 685 mch_memmove(newp + offset + spaces, s, (size_t)s_len); 686 offset += s_len; 687 688 if (spaces && !bdp->is_short) 689 { 690 /* insert post-padding */ 691 vim_memset(newp + offset + spaces, ' ', (size_t)(p_ts - spaces)); 692 /* We're splitting a TAB, don't copy it. */ 693 oldp++; 694 /* We allowed for that TAB, remember this now */ 695 count++; 696 } 697 698 if (spaces > 0) 699 offset += count; 700 STRMOVE(newp + offset, oldp); 701 702 ml_replace(lnum, newp, FALSE); 703 704 if (lnum == oap->end.lnum) 705 { 706 /* Set "']" mark to the end of the block instead of the end of 707 * the insert in the first line. */ 708 curbuf->b_op_end.lnum = oap->end.lnum; 709 curbuf->b_op_end.col = offset; 710 } 711 } /* for all lnum */ 712 713 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); 714 715 State = oldstate; 716 } 717 #endif 718 719 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) 720 /* 721 * op_reindent - handle reindenting a block of lines. 722 */ 723 void 724 op_reindent(oparg_T *oap, int (*how)(void)) 725 { 726 long i; 727 char_u *l; 728 int amount; 729 linenr_T first_changed = 0; 730 linenr_T last_changed = 0; 731 linenr_T start_lnum = curwin->w_cursor.lnum; 732 733 /* Don't even try when 'modifiable' is off. */ 734 if (!curbuf->b_p_ma) 735 { 736 EMSG(_(e_modifiable)); 737 return; 738 } 739 740 for (i = oap->line_count; --i >= 0 && !got_int; ) 741 { 742 /* it's a slow thing to do, so give feedback so there's no worry that 743 * the computer's just hung. */ 744 745 if (i > 1 746 && (i % 50 == 0 || i == oap->line_count - 1) 747 && oap->line_count > p_report) 748 smsg((char_u *)_("%ld lines to indent... "), i); 749 750 /* 751 * Be vi-compatible: For lisp indenting the first line is not 752 * indented, unless there is only one line. 753 */ 754 #ifdef FEAT_LISP 755 if (i != oap->line_count - 1 || oap->line_count == 1 756 || how != get_lisp_indent) 757 #endif 758 { 759 l = skipwhite(ml_get_curline()); 760 if (*l == NUL) /* empty or blank line */ 761 amount = 0; 762 else 763 amount = how(); /* get the indent for this line */ 764 765 if (amount >= 0 && set_indent(amount, SIN_UNDO)) 766 { 767 /* did change the indent, call changed_lines() later */ 768 if (first_changed == 0) 769 first_changed = curwin->w_cursor.lnum; 770 last_changed = curwin->w_cursor.lnum; 771 } 772 } 773 ++curwin->w_cursor.lnum; 774 curwin->w_cursor.col = 0; /* make sure it's valid */ 775 } 776 777 /* put cursor on first non-blank of indented line */ 778 curwin->w_cursor.lnum = start_lnum; 779 beginline(BL_SOL | BL_FIX); 780 781 /* Mark changed lines so that they will be redrawn. When Visual 782 * highlighting was present, need to continue until the last line. When 783 * there is no change still need to remove the Visual highlighting. */ 784 if (last_changed != 0) 785 changed_lines(first_changed, 0, 786 oap->is_VIsual ? start_lnum + oap->line_count : 787 last_changed + 1, 0L); 788 else if (oap->is_VIsual) 789 redraw_curbuf_later(INVERTED); 790 791 if (oap->line_count > p_report) 792 { 793 i = oap->line_count - (i + 1); 794 if (i == 1) 795 MSG(_("1 line indented ")); 796 else 797 smsg((char_u *)_("%ld lines indented "), i); 798 } 799 /* set '[ and '] marks */ 800 curbuf->b_op_start = oap->start; 801 curbuf->b_op_end = oap->end; 802 } 803 #endif /* defined(FEAT_LISP) || defined(FEAT_CINDENT) */ 804 805 #if defined(FEAT_EVAL) || defined(PROTO) 806 /* 807 * Keep the last expression line here, for repeating. 808 */ 809 static char_u *expr_line = NULL; 810 811 /* 812 * Get an expression for the "\"=expr1" or "CTRL-R =expr1" 813 * Returns '=' when OK, NUL otherwise. 814 */ 815 int 816 get_expr_register(void) 817 { 818 char_u *new_line; 819 820 new_line = getcmdline('=', 0L, 0); 821 if (new_line == NULL) 822 return NUL; 823 if (*new_line == NUL) /* use previous line */ 824 vim_free(new_line); 825 else 826 set_expr_line(new_line); 827 return '='; 828 } 829 830 /* 831 * Set the expression for the '=' register. 832 * Argument must be an allocated string. 833 */ 834 void 835 set_expr_line(char_u *new_line) 836 { 837 vim_free(expr_line); 838 expr_line = new_line; 839 } 840 841 /* 842 * Get the result of the '=' register expression. 843 * Returns a pointer to allocated memory, or NULL for failure. 844 */ 845 char_u * 846 get_expr_line(void) 847 { 848 char_u *expr_copy; 849 char_u *rv; 850 static int nested = 0; 851 852 if (expr_line == NULL) 853 return NULL; 854 855 /* Make a copy of the expression, because evaluating it may cause it to be 856 * changed. */ 857 expr_copy = vim_strsave(expr_line); 858 if (expr_copy == NULL) 859 return NULL; 860 861 /* When we are invoked recursively limit the evaluation to 10 levels. 862 * Then return the string as-is. */ 863 if (nested >= 10) 864 return expr_copy; 865 866 ++nested; 867 rv = eval_to_string(expr_copy, NULL, TRUE); 868 --nested; 869 vim_free(expr_copy); 870 return rv; 871 } 872 873 /* 874 * Get the '=' register expression itself, without evaluating it. 875 */ 876 char_u * 877 get_expr_line_src(void) 878 { 879 if (expr_line == NULL) 880 return NULL; 881 return vim_strsave(expr_line); 882 } 883 #endif /* FEAT_EVAL */ 884 885 /* 886 * Check if 'regname' is a valid name of a yank register. 887 * Note: There is no check for 0 (default register), caller should do this 888 */ 889 int 890 valid_yank_reg( 891 int regname, 892 int writing) /* if TRUE check for writable registers */ 893 { 894 if ( (regname > 0 && ASCII_ISALNUM(regname)) 895 || (!writing && vim_strchr((char_u *) 896 #ifdef FEAT_EVAL 897 "/.%:=" 898 #else 899 "/.%:" 900 #endif 901 , regname) != NULL) 902 || regname == '#' 903 || regname == '"' 904 || regname == '-' 905 || regname == '_' 906 #ifdef FEAT_CLIPBOARD 907 || regname == '*' 908 || regname == '+' 909 #endif 910 #ifdef FEAT_DND 911 || (!writing && regname == '~') 912 #endif 913 ) 914 return TRUE; 915 return FALSE; 916 } 917 918 /* 919 * Set y_current and y_append, according to the value of "regname". 920 * Cannot handle the '_' register. 921 * Must only be called with a valid register name! 922 * 923 * If regname is 0 and writing, use register 0 924 * If regname is 0 and reading, use previous register 925 * 926 * Return TRUE when the register should be inserted literally (selection or 927 * clipboard). 928 */ 929 int 930 get_yank_register(int regname, int writing) 931 { 932 int i; 933 int ret = FALSE; 934 935 y_append = FALSE; 936 if ((regname == 0 || regname == '"') && !writing && y_previous != NULL) 937 { 938 y_current = y_previous; 939 return ret; 940 } 941 i = regname; 942 if (VIM_ISDIGIT(i)) 943 i -= '0'; 944 else if (ASCII_ISLOWER(i)) 945 i = CharOrdLow(i) + 10; 946 else if (ASCII_ISUPPER(i)) 947 { 948 i = CharOrdUp(i) + 10; 949 y_append = TRUE; 950 } 951 else if (regname == '-') 952 i = DELETION_REGISTER; 953 #ifdef FEAT_CLIPBOARD 954 /* When selection is not available, use register 0 instead of '*' */ 955 else if (clip_star.available && regname == '*') 956 { 957 i = STAR_REGISTER; 958 ret = TRUE; 959 } 960 /* When clipboard is not available, use register 0 instead of '+' */ 961 else if (clip_plus.available && regname == '+') 962 { 963 i = PLUS_REGISTER; 964 ret = TRUE; 965 } 966 #endif 967 #ifdef FEAT_DND 968 else if (!writing && regname == '~') 969 i = TILDE_REGISTER; 970 #endif 971 else /* not 0-9, a-z, A-Z or '-': use register 0 */ 972 i = 0; 973 y_current = &(y_regs[i]); 974 if (writing) /* remember the register we write into for do_put() */ 975 y_previous = y_current; 976 return ret; 977 } 978 979 #if defined(FEAT_CLIPBOARD) || defined(PROTO) 980 /* 981 * When "regname" is a clipboard register, obtain the selection. If it's not 982 * available return zero, otherwise return "regname". 983 */ 984 int 985 may_get_selection(int regname) 986 { 987 if (regname == '*') 988 { 989 if (!clip_star.available) 990 regname = 0; 991 else 992 clip_get_selection(&clip_star); 993 } 994 else if (regname == '+') 995 { 996 if (!clip_plus.available) 997 regname = 0; 998 else 999 clip_get_selection(&clip_plus); 1000 } 1001 return regname; 1002 } 1003 #endif 1004 1005 /* 1006 * Obtain the contents of a "normal" register. The register is made empty. 1007 * The returned pointer has allocated memory, use put_register() later. 1008 */ 1009 void * 1010 get_register( 1011 int name, 1012 int copy) /* make a copy, if FALSE make register empty. */ 1013 { 1014 yankreg_T *reg; 1015 int i; 1016 1017 #ifdef FEAT_CLIPBOARD 1018 /* When Visual area changed, may have to update selection. Obtain the 1019 * selection too. */ 1020 if (name == '*' && clip_star.available) 1021 { 1022 if (clip_isautosel_star()) 1023 clip_update_selection(&clip_star); 1024 may_get_selection(name); 1025 } 1026 if (name == '+' && clip_plus.available) 1027 { 1028 if (clip_isautosel_plus()) 1029 clip_update_selection(&clip_plus); 1030 may_get_selection(name); 1031 } 1032 #endif 1033 1034 get_yank_register(name, 0); 1035 reg = (yankreg_T *)alloc((unsigned)sizeof(yankreg_T)); 1036 if (reg != NULL) 1037 { 1038 *reg = *y_current; 1039 if (copy) 1040 { 1041 /* If we run out of memory some or all of the lines are empty. */ 1042 if (reg->y_size == 0) 1043 reg->y_array = NULL; 1044 else 1045 reg->y_array = (char_u **)alloc((unsigned)(sizeof(char_u *) 1046 * reg->y_size)); 1047 if (reg->y_array != NULL) 1048 { 1049 for (i = 0; i < reg->y_size; ++i) 1050 reg->y_array[i] = vim_strsave(y_current->y_array[i]); 1051 } 1052 } 1053 else 1054 y_current->y_array = NULL; 1055 } 1056 return (void *)reg; 1057 } 1058 1059 /* 1060 * Put "reg" into register "name". Free any previous contents and "reg". 1061 */ 1062 void 1063 put_register(int name, void *reg) 1064 { 1065 get_yank_register(name, 0); 1066 free_yank_all(); 1067 *y_current = *(yankreg_T *)reg; 1068 vim_free(reg); 1069 1070 #ifdef FEAT_CLIPBOARD 1071 /* Send text written to clipboard register to the clipboard. */ 1072 may_set_selection(); 1073 #endif 1074 } 1075 1076 void 1077 free_register(void *reg) 1078 { 1079 yankreg_T tmp; 1080 1081 tmp = *y_current; 1082 *y_current = *(yankreg_T *)reg; 1083 free_yank_all(); 1084 vim_free(reg); 1085 *y_current = tmp; 1086 } 1087 1088 #if defined(FEAT_MOUSE) || defined(PROTO) 1089 /* 1090 * return TRUE if the current yank register has type MLINE 1091 */ 1092 int 1093 yank_register_mline(int regname) 1094 { 1095 if (regname != 0 && !valid_yank_reg(regname, FALSE)) 1096 return FALSE; 1097 if (regname == '_') /* black hole is always empty */ 1098 return FALSE; 1099 get_yank_register(regname, FALSE); 1100 return (y_current->y_type == MLINE); 1101 } 1102 #endif 1103 1104 /* 1105 * Start or stop recording into a yank register. 1106 * 1107 * Return FAIL for failure, OK otherwise. 1108 */ 1109 int 1110 do_record(int c) 1111 { 1112 char_u *p; 1113 static int regname; 1114 yankreg_T *old_y_previous, *old_y_current; 1115 int retval; 1116 1117 if (reg_recording == 0) /* start recording */ 1118 { 1119 /* registers 0-9, a-z and " are allowed */ 1120 if (c < 0 || (!ASCII_ISALNUM(c) && c != '"')) 1121 retval = FAIL; 1122 else 1123 { 1124 reg_recording = c; 1125 showmode(); 1126 regname = c; 1127 retval = OK; 1128 } 1129 } 1130 else /* stop recording */ 1131 { 1132 /* 1133 * Get the recorded key hits. K_SPECIAL and CSI will be escaped, this 1134 * needs to be removed again to put it in a register. exec_reg then 1135 * adds the escaping back later. 1136 */ 1137 reg_recording = 0; 1138 MSG(""); 1139 p = get_recorded(); 1140 if (p == NULL) 1141 retval = FAIL; 1142 else 1143 { 1144 /* Remove escaping for CSI and K_SPECIAL in multi-byte chars. */ 1145 vim_unescape_csi(p); 1146 1147 /* 1148 * We don't want to change the default register here, so save and 1149 * restore the current register name. 1150 */ 1151 old_y_previous = y_previous; 1152 old_y_current = y_current; 1153 1154 retval = stuff_yank(regname, p); 1155 1156 y_previous = old_y_previous; 1157 y_current = old_y_current; 1158 } 1159 } 1160 return retval; 1161 } 1162 1163 /* 1164 * Stuff string "p" into yank register "regname" as a single line (append if 1165 * uppercase). "p" must have been alloced. 1166 * 1167 * return FAIL for failure, OK otherwise 1168 */ 1169 static int 1170 stuff_yank(int regname, char_u *p) 1171 { 1172 char_u *lp; 1173 char_u **pp; 1174 1175 /* check for read-only register */ 1176 if (regname != 0 && !valid_yank_reg(regname, TRUE)) 1177 { 1178 vim_free(p); 1179 return FAIL; 1180 } 1181 if (regname == '_') /* black hole: don't do anything */ 1182 { 1183 vim_free(p); 1184 return OK; 1185 } 1186 get_yank_register(regname, TRUE); 1187 if (y_append && y_current->y_array != NULL) 1188 { 1189 pp = &(y_current->y_array[y_current->y_size - 1]); 1190 lp = lalloc((long_u)(STRLEN(*pp) + STRLEN(p) + 1), TRUE); 1191 if (lp == NULL) 1192 { 1193 vim_free(p); 1194 return FAIL; 1195 } 1196 STRCPY(lp, *pp); 1197 STRCAT(lp, p); 1198 vim_free(p); 1199 vim_free(*pp); 1200 *pp = lp; 1201 } 1202 else 1203 { 1204 free_yank_all(); 1205 if ((y_current->y_array = 1206 (char_u **)alloc((unsigned)sizeof(char_u *))) == NULL) 1207 { 1208 vim_free(p); 1209 return FAIL; 1210 } 1211 y_current->y_array[0] = p; 1212 y_current->y_size = 1; 1213 y_current->y_type = MCHAR; /* used to be MLINE, why? */ 1214 #ifdef FEAT_VIMINFO 1215 y_current->y_time_set = vim_time(); 1216 #endif 1217 } 1218 return OK; 1219 } 1220 1221 static int execreg_lastc = NUL; 1222 1223 /* 1224 * Execute a yank register: copy it into the stuff buffer. 1225 * 1226 * Return FAIL for failure, OK otherwise. 1227 */ 1228 int 1229 do_execreg( 1230 int regname, 1231 int colon, /* insert ':' before each line */ 1232 int addcr, /* always add '\n' to end of line */ 1233 int silent) /* set "silent" flag in typeahead buffer */ 1234 { 1235 long i; 1236 char_u *p; 1237 int retval = OK; 1238 int remap; 1239 1240 if (regname == '@') /* repeat previous one */ 1241 { 1242 if (execreg_lastc == NUL) 1243 { 1244 EMSG(_("E748: No previously used register")); 1245 return FAIL; 1246 } 1247 regname = execreg_lastc; 1248 } 1249 /* check for valid regname */ 1250 if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE)) 1251 { 1252 emsg_invreg(regname); 1253 return FAIL; 1254 } 1255 execreg_lastc = regname; 1256 1257 #ifdef FEAT_CLIPBOARD 1258 regname = may_get_selection(regname); 1259 #endif 1260 1261 if (regname == '_') /* black hole: don't stuff anything */ 1262 return OK; 1263 1264 #ifdef FEAT_CMDHIST 1265 if (regname == ':') /* use last command line */ 1266 { 1267 if (last_cmdline == NULL) 1268 { 1269 EMSG(_(e_nolastcmd)); 1270 return FAIL; 1271 } 1272 VIM_CLEAR(new_last_cmdline); /* don't keep the cmdline containing @: */ 1273 /* Escape all control characters with a CTRL-V */ 1274 p = vim_strsave_escaped_ext(last_cmdline, 1275 (char_u *)"\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037", Ctrl_V, FALSE); 1276 if (p != NULL) 1277 { 1278 /* When in Visual mode "'<,'>" will be prepended to the command. 1279 * Remove it when it's already there. */ 1280 if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) 1281 retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); 1282 else 1283 retval = put_in_typebuf(p, TRUE, TRUE, silent); 1284 } 1285 vim_free(p); 1286 } 1287 #endif 1288 #ifdef FEAT_EVAL 1289 else if (regname == '=') 1290 { 1291 p = get_expr_line(); 1292 if (p == NULL) 1293 return FAIL; 1294 retval = put_in_typebuf(p, TRUE, colon, silent); 1295 vim_free(p); 1296 } 1297 #endif 1298 else if (regname == '.') /* use last inserted text */ 1299 { 1300 p = get_last_insert_save(); 1301 if (p == NULL) 1302 { 1303 EMSG(_(e_noinstext)); 1304 return FAIL; 1305 } 1306 retval = put_in_typebuf(p, FALSE, colon, silent); 1307 vim_free(p); 1308 } 1309 else 1310 { 1311 get_yank_register(regname, FALSE); 1312 if (y_current->y_array == NULL) 1313 return FAIL; 1314 1315 /* Disallow remaping for ":@r". */ 1316 remap = colon ? REMAP_NONE : REMAP_YES; 1317 1318 /* 1319 * Insert lines into typeahead buffer, from last one to first one. 1320 */ 1321 put_reedit_in_typebuf(silent); 1322 for (i = y_current->y_size; --i >= 0; ) 1323 { 1324 char_u *escaped; 1325 1326 /* insert NL between lines and after last line if type is MLINE */ 1327 if (y_current->y_type == MLINE || i < y_current->y_size - 1 1328 || addcr) 1329 { 1330 if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL) 1331 return FAIL; 1332 } 1333 escaped = vim_strsave_escape_csi(y_current->y_array[i]); 1334 if (escaped == NULL) 1335 return FAIL; 1336 retval = ins_typebuf(escaped, remap, 0, TRUE, silent); 1337 vim_free(escaped); 1338 if (retval == FAIL) 1339 return FAIL; 1340 if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent) 1341 == FAIL) 1342 return FAIL; 1343 } 1344 reg_executing = regname == 0 ? '"' : regname; // disable "q" command 1345 } 1346 return retval; 1347 } 1348 1349 /* 1350 * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's 1351 * used only after other typeahead has been processed. 1352 */ 1353 static void 1354 put_reedit_in_typebuf(int silent) 1355 { 1356 char_u buf[3]; 1357 1358 if (restart_edit != NUL) 1359 { 1360 if (restart_edit == 'V') 1361 { 1362 buf[0] = 'g'; 1363 buf[1] = 'R'; 1364 buf[2] = NUL; 1365 } 1366 else 1367 { 1368 buf[0] = restart_edit == 'I' ? 'i' : restart_edit; 1369 buf[1] = NUL; 1370 } 1371 if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK) 1372 restart_edit = NUL; 1373 } 1374 } 1375 1376 /* 1377 * Insert register contents "s" into the typeahead buffer, so that it will be 1378 * executed again. 1379 * When "esc" is TRUE it is to be taken literally: Escape CSI characters and 1380 * no remapping. 1381 */ 1382 static int 1383 put_in_typebuf( 1384 char_u *s, 1385 int esc, 1386 int colon, /* add ':' before the line */ 1387 int silent) 1388 { 1389 int retval = OK; 1390 1391 put_reedit_in_typebuf(silent); 1392 if (colon) 1393 retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent); 1394 if (retval == OK) 1395 { 1396 char_u *p; 1397 1398 if (esc) 1399 p = vim_strsave_escape_csi(s); 1400 else 1401 p = s; 1402 if (p == NULL) 1403 retval = FAIL; 1404 else 1405 retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES, 1406 0, TRUE, silent); 1407 if (esc) 1408 vim_free(p); 1409 } 1410 if (colon && retval == OK) 1411 retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent); 1412 return retval; 1413 } 1414 1415 /* 1416 * Insert a yank register: copy it into the Read buffer. 1417 * Used by CTRL-R command and middle mouse button in insert mode. 1418 * 1419 * return FAIL for failure, OK otherwise 1420 */ 1421 int 1422 insert_reg( 1423 int regname, 1424 int literally_arg) /* insert literally, not as if typed */ 1425 { 1426 long i; 1427 int retval = OK; 1428 char_u *arg; 1429 int allocated; 1430 int literally = literally_arg; 1431 1432 /* 1433 * It is possible to get into an endless loop by having CTRL-R a in 1434 * register a and then, in insert mode, doing CTRL-R a. 1435 * If you hit CTRL-C, the loop will be broken here. 1436 */ 1437 ui_breakcheck(); 1438 if (got_int) 1439 return FAIL; 1440 1441 /* check for valid regname */ 1442 if (regname != NUL && !valid_yank_reg(regname, FALSE)) 1443 return FAIL; 1444 1445 #ifdef FEAT_CLIPBOARD 1446 regname = may_get_selection(regname); 1447 #endif 1448 1449 if (regname == '.') /* insert last inserted text */ 1450 retval = stuff_inserted(NUL, 1L, TRUE); 1451 else if (get_spec_reg(regname, &arg, &allocated, TRUE)) 1452 { 1453 if (arg == NULL) 1454 return FAIL; 1455 stuffescaped(arg, literally); 1456 if (allocated) 1457 vim_free(arg); 1458 } 1459 else /* name or number register */ 1460 { 1461 if (get_yank_register(regname, FALSE)) 1462 literally = TRUE; 1463 if (y_current->y_array == NULL) 1464 retval = FAIL; 1465 else 1466 { 1467 for (i = 0; i < y_current->y_size; ++i) 1468 { 1469 stuffescaped(y_current->y_array[i], literally); 1470 /* 1471 * Insert a newline between lines and after last line if 1472 * y_type is MLINE. 1473 */ 1474 if (y_current->y_type == MLINE || i < y_current->y_size - 1) 1475 stuffcharReadbuff('\n'); 1476 } 1477 } 1478 } 1479 1480 return retval; 1481 } 1482 1483 /* 1484 * Stuff a string into the typeahead buffer, such that edit() will insert it 1485 * literally ("literally" TRUE) or interpret is as typed characters. 1486 */ 1487 static void 1488 stuffescaped(char_u *arg, int literally) 1489 { 1490 int c; 1491 char_u *start; 1492 1493 while (*arg != NUL) 1494 { 1495 /* Stuff a sequence of normal ASCII characters, that's fast. Also 1496 * stuff K_SPECIAL to get the effect of a special key when "literally" 1497 * is TRUE. */ 1498 start = arg; 1499 while ((*arg >= ' ' 1500 #ifndef EBCDIC 1501 && *arg < DEL /* EBCDIC: chars above space are normal */ 1502 #endif 1503 ) 1504 || (*arg == K_SPECIAL && !literally)) 1505 ++arg; 1506 if (arg > start) 1507 stuffReadbuffLen(start, (long)(arg - start)); 1508 1509 /* stuff a single special character */ 1510 if (*arg != NUL) 1511 { 1512 #ifdef FEAT_MBYTE 1513 if (has_mbyte) 1514 c = mb_cptr2char_adv(&arg); 1515 else 1516 #endif 1517 c = *arg++; 1518 if (literally && ((c < ' ' && c != TAB) || c == DEL)) 1519 stuffcharReadbuff(Ctrl_V); 1520 stuffcharReadbuff(c); 1521 } 1522 } 1523 } 1524 1525 /* 1526 * If "regname" is a special register, return TRUE and store a pointer to its 1527 * value in "argp". 1528 */ 1529 int 1530 get_spec_reg( 1531 int regname, 1532 char_u **argp, 1533 int *allocated, /* return: TRUE when value was allocated */ 1534 int errmsg) /* give error message when failing */ 1535 { 1536 int cnt; 1537 1538 *argp = NULL; 1539 *allocated = FALSE; 1540 switch (regname) 1541 { 1542 case '%': /* file name */ 1543 if (errmsg) 1544 check_fname(); /* will give emsg if not set */ 1545 *argp = curbuf->b_fname; 1546 return TRUE; 1547 1548 case '#': /* alternate file name */ 1549 *argp = getaltfname(errmsg); /* may give emsg if not set */ 1550 return TRUE; 1551 1552 #ifdef FEAT_EVAL 1553 case '=': /* result of expression */ 1554 *argp = get_expr_line(); 1555 *allocated = TRUE; 1556 return TRUE; 1557 #endif 1558 1559 case ':': /* last command line */ 1560 if (last_cmdline == NULL && errmsg) 1561 EMSG(_(e_nolastcmd)); 1562 *argp = last_cmdline; 1563 return TRUE; 1564 1565 case '/': /* last search-pattern */ 1566 if (last_search_pat() == NULL && errmsg) 1567 EMSG(_(e_noprevre)); 1568 *argp = last_search_pat(); 1569 return TRUE; 1570 1571 case '.': /* last inserted text */ 1572 *argp = get_last_insert_save(); 1573 *allocated = TRUE; 1574 if (*argp == NULL && errmsg) 1575 EMSG(_(e_noinstext)); 1576 return TRUE; 1577 1578 #ifdef FEAT_SEARCHPATH 1579 case Ctrl_F: /* Filename under cursor */ 1580 case Ctrl_P: /* Path under cursor, expand via "path" */ 1581 if (!errmsg) 1582 return FALSE; 1583 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP 1584 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL); 1585 *allocated = TRUE; 1586 return TRUE; 1587 #endif 1588 1589 case Ctrl_W: /* word under cursor */ 1590 case Ctrl_A: /* WORD (mnemonic All) under cursor */ 1591 if (!errmsg) 1592 return FALSE; 1593 cnt = find_ident_under_cursor(argp, regname == Ctrl_W 1594 ? (FIND_IDENT|FIND_STRING) : FIND_STRING); 1595 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL; 1596 *allocated = TRUE; 1597 return TRUE; 1598 1599 case Ctrl_L: /* Line under cursor */ 1600 if (!errmsg) 1601 return FALSE; 1602 1603 *argp = ml_get_buf(curwin->w_buffer, 1604 curwin->w_cursor.lnum, FALSE); 1605 return TRUE; 1606 1607 case '_': /* black hole: always empty */ 1608 *argp = (char_u *)""; 1609 return TRUE; 1610 } 1611 1612 return FALSE; 1613 } 1614 1615 /* 1616 * Paste a yank register into the command line. 1617 * Only for non-special registers. 1618 * Used by CTRL-R command in command-line mode 1619 * insert_reg() can't be used here, because special characters from the 1620 * register contents will be interpreted as commands. 1621 * 1622 * return FAIL for failure, OK otherwise 1623 */ 1624 int 1625 cmdline_paste_reg( 1626 int regname, 1627 int literally_arg, /* Insert text literally instead of "as typed" */ 1628 int remcr) /* don't add CR characters */ 1629 { 1630 long i; 1631 int literally = literally_arg; 1632 1633 if (get_yank_register(regname, FALSE)) 1634 literally = TRUE; 1635 if (y_current->y_array == NULL) 1636 return FAIL; 1637 1638 for (i = 0; i < y_current->y_size; ++i) 1639 { 1640 cmdline_paste_str(y_current->y_array[i], literally); 1641 1642 /* Insert ^M between lines and after last line if type is MLINE. 1643 * Don't do this when "remcr" is TRUE. */ 1644 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr) 1645 cmdline_paste_str((char_u *)"\r", literally); 1646 1647 /* Check for CTRL-C, in case someone tries to paste a few thousand 1648 * lines and gets bored. */ 1649 ui_breakcheck(); 1650 if (got_int) 1651 return FAIL; 1652 } 1653 return OK; 1654 } 1655 1656 #if defined(FEAT_CLIPBOARD) || defined(PROTO) 1657 /* 1658 * Adjust the register name pointed to with "rp" for the clipboard being 1659 * used always and the clipboard being available. 1660 */ 1661 void 1662 adjust_clip_reg(int *rp) 1663 { 1664 /* If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard', 1665 * use '*' or '+' reg, respectively. "unnamedplus" prevails. */ 1666 if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0)) 1667 { 1668 if (clip_unnamed != 0) 1669 *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available) 1670 ? '+' : '*'; 1671 else 1672 *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available) 1673 ? '+' : '*'; 1674 } 1675 if (!clip_star.available && *rp == '*') 1676 *rp = 0; 1677 if (!clip_plus.available && *rp == '+') 1678 *rp = 0; 1679 } 1680 #endif 1681 1682 /* 1683 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc. 1684 */ 1685 void 1686 shift_delete_registers() 1687 { 1688 int n; 1689 1690 y_current = &y_regs[9]; 1691 free_yank_all(); /* free register nine */ 1692 for (n = 9; n > 1; --n) 1693 y_regs[n] = y_regs[n - 1]; 1694 y_current = &y_regs[1]; 1695 if (!y_append) 1696 y_previous = y_current; 1697 y_regs[1].y_array = NULL; /* set register one to empty */ 1698 } 1699 1700 #if defined(FEAT_EVAL) 1701 static void 1702 yank_do_autocmd(oparg_T *oap, yankreg_T *reg) 1703 { 1704 static int recursive = FALSE; 1705 dict_T *v_event; 1706 list_T *list; 1707 int n; 1708 char_u buf[NUMBUFLEN + 2]; 1709 long reglen = 0; 1710 1711 if (recursive) 1712 return; 1713 1714 v_event = get_vim_var_dict(VV_EVENT); 1715 1716 list = list_alloc(); 1717 if (list == NULL) 1718 return; 1719 for (n = 0; n < reg->y_size; n++) 1720 list_append_string(list, reg->y_array[n], -1); 1721 list->lv_lock = VAR_FIXED; 1722 dict_add_list(v_event, "regcontents", list); 1723 1724 buf[0] = (char_u)oap->regname; 1725 buf[1] = NUL; 1726 dict_add_nr_str(v_event, "regname", 0, buf); 1727 1728 buf[0] = get_op_char(oap->op_type); 1729 buf[1] = get_extra_op_char(oap->op_type); 1730 buf[2] = NUL; 1731 dict_add_nr_str(v_event, "operator", 0, buf); 1732 1733 buf[0] = NUL; 1734 buf[1] = NUL; 1735 switch (get_reg_type(oap->regname, ®len)) 1736 { 1737 case MLINE: buf[0] = 'V'; break; 1738 case MCHAR: buf[0] = 'v'; break; 1739 case MBLOCK: 1740 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V, 1741 reglen + 1); 1742 break; 1743 } 1744 dict_add_nr_str(v_event, "regtype", 0, buf); 1745 1746 /* Lock the dictionary and its keys */ 1747 dict_set_items_ro(v_event); 1748 1749 recursive = TRUE; 1750 textlock++; 1751 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf); 1752 textlock--; 1753 recursive = FALSE; 1754 1755 /* Empty the dictionary, v:event is still valid */ 1756 dict_free_contents(v_event); 1757 hash_init(&v_event->dv_hashtab); 1758 } 1759 #endif 1760 1761 /* 1762 * Handle a delete operation. 1763 * 1764 * Return FAIL if undo failed, OK otherwise. 1765 */ 1766 int 1767 op_delete(oparg_T *oap) 1768 { 1769 int n; 1770 linenr_T lnum; 1771 char_u *ptr; 1772 char_u *newp, *oldp; 1773 struct block_def bd; 1774 linenr_T old_lcount = curbuf->b_ml.ml_line_count; 1775 int did_yank = FALSE; 1776 int orig_regname = oap->regname; 1777 1778 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to do */ 1779 return OK; 1780 1781 /* Nothing to delete, return here. Do prepare undo, for op_change(). */ 1782 if (oap->empty) 1783 return u_save_cursor(); 1784 1785 if (!curbuf->b_p_ma) 1786 { 1787 EMSG(_(e_modifiable)); 1788 return FAIL; 1789 } 1790 1791 #ifdef FEAT_CLIPBOARD 1792 adjust_clip_reg(&oap->regname); 1793 #endif 1794 1795 #ifdef FEAT_MBYTE 1796 if (has_mbyte) 1797 mb_adjust_opend(oap); 1798 #endif 1799 1800 /* 1801 * Imitate the strange Vi behaviour: If the delete spans more than one 1802 * line and motion_type == MCHAR and the result is a blank line, make the 1803 * delete linewise. Don't do this for the change command or Visual mode. 1804 */ 1805 if ( oap->motion_type == MCHAR 1806 && !oap->is_VIsual 1807 && !oap->block_mode 1808 && oap->line_count > 1 1809 && oap->motion_force == NUL 1810 && oap->op_type == OP_DELETE) 1811 { 1812 ptr = ml_get(oap->end.lnum) + oap->end.col; 1813 if (*ptr != NUL) 1814 ptr += oap->inclusive; 1815 ptr = skipwhite(ptr); 1816 if (*ptr == NUL && inindent(0)) 1817 oap->motion_type = MLINE; 1818 } 1819 1820 /* 1821 * Check for trying to delete (e.g. "D") in an empty line. 1822 * Note: For the change operator it is ok. 1823 */ 1824 if ( oap->motion_type == MCHAR 1825 && oap->line_count == 1 1826 && oap->op_type == OP_DELETE 1827 && *ml_get(oap->start.lnum) == NUL) 1828 { 1829 /* 1830 * It's an error to operate on an empty region, when 'E' included in 1831 * 'cpoptions' (Vi compatible). 1832 */ 1833 #ifdef FEAT_VIRTUALEDIT 1834 if (virtual_op) 1835 /* Virtual editing: Nothing gets deleted, but we set the '[ and '] 1836 * marks as if it happened. */ 1837 goto setmarks; 1838 #endif 1839 if (vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL) 1840 beep_flush(); 1841 return OK; 1842 } 1843 1844 /* 1845 * Do a yank of whatever we're about to delete. 1846 * If a yank register was specified, put the deleted text into that 1847 * register. For the black hole register '_' don't yank anything. 1848 */ 1849 if (oap->regname != '_') 1850 { 1851 if (oap->regname != 0) 1852 { 1853 /* check for read-only register */ 1854 if (!valid_yank_reg(oap->regname, TRUE)) 1855 { 1856 beep_flush(); 1857 return OK; 1858 } 1859 get_yank_register(oap->regname, TRUE); /* yank into specif'd reg. */ 1860 if (op_yank(oap, TRUE, FALSE) == OK) /* yank without message */ 1861 did_yank = TRUE; 1862 } 1863 1864 /* 1865 * Put deleted text into register 1 and shift number registers if the 1866 * delete contains a line break, or when a regname has been specified. 1867 * Use the register name from before adjust_clip_reg() may have 1868 * changed it. 1869 */ 1870 if (orig_regname != 0 || oap->motion_type == MLINE 1871 || oap->line_count > 1 || oap->use_reg_one) 1872 { 1873 shift_delete_registers(); 1874 if (op_yank(oap, TRUE, FALSE) == OK) 1875 did_yank = TRUE; 1876 } 1877 1878 /* Yank into small delete register when no named register specified 1879 * and the delete is within one line. */ 1880 if (( 1881 #ifdef FEAT_CLIPBOARD 1882 ((clip_unnamed & CLIP_UNNAMED) && oap->regname == '*') || 1883 ((clip_unnamed & CLIP_UNNAMED_PLUS) && oap->regname == '+') || 1884 #endif 1885 oap->regname == 0) && oap->motion_type != MLINE 1886 && oap->line_count == 1) 1887 { 1888 oap->regname = '-'; 1889 get_yank_register(oap->regname, TRUE); 1890 if (op_yank(oap, TRUE, FALSE) == OK) 1891 did_yank = TRUE; 1892 oap->regname = 0; 1893 } 1894 1895 /* 1896 * If there's too much stuff to fit in the yank register, then get a 1897 * confirmation before doing the delete. This is crude, but simple. 1898 * And it avoids doing a delete of something we can't put back if we 1899 * want. 1900 */ 1901 if (!did_yank) 1902 { 1903 int msg_silent_save = msg_silent; 1904 1905 msg_silent = 0; /* must display the prompt */ 1906 n = ask_yesno((char_u *)_("cannot yank; delete anyway"), TRUE); 1907 msg_silent = msg_silent_save; 1908 if (n != 'y') 1909 { 1910 EMSG(_(e_abort)); 1911 return FAIL; 1912 } 1913 } 1914 1915 #if defined(FEAT_EVAL) 1916 if (did_yank && has_textyankpost()) 1917 yank_do_autocmd(oap, y_current); 1918 #endif 1919 } 1920 1921 /* 1922 * block mode delete 1923 */ 1924 if (oap->block_mode) 1925 { 1926 if (u_save((linenr_T)(oap->start.lnum - 1), 1927 (linenr_T)(oap->end.lnum + 1)) == FAIL) 1928 return FAIL; 1929 1930 for (lnum = curwin->w_cursor.lnum; lnum <= oap->end.lnum; ++lnum) 1931 { 1932 block_prep(oap, &bd, lnum, TRUE); 1933 if (bd.textlen == 0) /* nothing to delete */ 1934 continue; 1935 1936 /* Adjust cursor position for tab replaced by spaces and 'lbr'. */ 1937 if (lnum == curwin->w_cursor.lnum) 1938 { 1939 curwin->w_cursor.col = bd.textcol + bd.startspaces; 1940 # ifdef FEAT_VIRTUALEDIT 1941 curwin->w_cursor.coladd = 0; 1942 # endif 1943 } 1944 1945 /* n == number of chars deleted 1946 * If we delete a TAB, it may be replaced by several characters. 1947 * Thus the number of characters may increase! 1948 */ 1949 n = bd.textlen - bd.startspaces - bd.endspaces; 1950 oldp = ml_get(lnum); 1951 newp = alloc_check((unsigned)STRLEN(oldp) + 1 - n); 1952 if (newp == NULL) 1953 continue; 1954 /* copy up to deleted part */ 1955 mch_memmove(newp, oldp, (size_t)bd.textcol); 1956 /* insert spaces */ 1957 vim_memset(newp + bd.textcol, ' ', 1958 (size_t)(bd.startspaces + bd.endspaces)); 1959 /* copy the part after the deleted part */ 1960 oldp += bd.textcol + bd.textlen; 1961 STRMOVE(newp + bd.textcol + bd.startspaces + bd.endspaces, oldp); 1962 /* replace the line */ 1963 ml_replace(lnum, newp, FALSE); 1964 } 1965 1966 check_cursor_col(); 1967 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, 1968 oap->end.lnum + 1, 0L); 1969 oap->line_count = 0; /* no lines deleted */ 1970 } 1971 else if (oap->motion_type == MLINE) 1972 { 1973 if (oap->op_type == OP_CHANGE) 1974 { 1975 /* Delete the lines except the first one. Temporarily move the 1976 * cursor to the next line. Save the current line number, if the 1977 * last line is deleted it may be changed. 1978 */ 1979 if (oap->line_count > 1) 1980 { 1981 lnum = curwin->w_cursor.lnum; 1982 ++curwin->w_cursor.lnum; 1983 del_lines((long)(oap->line_count - 1), TRUE); 1984 curwin->w_cursor.lnum = lnum; 1985 } 1986 if (u_save_cursor() == FAIL) 1987 return FAIL; 1988 if (curbuf->b_p_ai) /* don't delete indent */ 1989 { 1990 beginline(BL_WHITE); /* cursor on first non-white */ 1991 did_ai = TRUE; /* delete the indent when ESC hit */ 1992 ai_col = curwin->w_cursor.col; 1993 } 1994 else 1995 beginline(0); /* cursor in column 0 */ 1996 truncate_line(FALSE); /* delete the rest of the line */ 1997 /* leave cursor past last char in line */ 1998 if (oap->line_count > 1) 1999 u_clearline(); /* "U" command not possible after "2cc" */ 2000 } 2001 else 2002 { 2003 del_lines(oap->line_count, TRUE); 2004 beginline(BL_WHITE | BL_FIX); 2005 u_clearline(); /* "U" command not possible after "dd" */ 2006 } 2007 } 2008 else 2009 { 2010 #ifdef FEAT_VIRTUALEDIT 2011 if (virtual_op) 2012 { 2013 int endcol = 0; 2014 2015 /* For virtualedit: break the tabs that are partly included. */ 2016 if (gchar_pos(&oap->start) == '\t') 2017 { 2018 if (u_save_cursor() == FAIL) /* save first line for undo */ 2019 return FAIL; 2020 if (oap->line_count == 1) 2021 endcol = getviscol2(oap->end.col, oap->end.coladd); 2022 coladvance_force(getviscol2(oap->start.col, oap->start.coladd)); 2023 oap->start = curwin->w_cursor; 2024 if (oap->line_count == 1) 2025 { 2026 coladvance(endcol); 2027 oap->end.col = curwin->w_cursor.col; 2028 oap->end.coladd = curwin->w_cursor.coladd; 2029 curwin->w_cursor = oap->start; 2030 } 2031 } 2032 2033 /* Break a tab only when it's included in the area. */ 2034 if (gchar_pos(&oap->end) == '\t' 2035 && (int)oap->end.coladd < oap->inclusive) 2036 { 2037 /* save last line for undo */ 2038 if (u_save((linenr_T)(oap->end.lnum - 1), 2039 (linenr_T)(oap->end.lnum + 1)) == FAIL) 2040 return FAIL; 2041 curwin->w_cursor = oap->end; 2042 coladvance_force(getviscol2(oap->end.col, oap->end.coladd)); 2043 oap->end = curwin->w_cursor; 2044 curwin->w_cursor = oap->start; 2045 } 2046 } 2047 #endif 2048 2049 if (oap->line_count == 1) /* delete characters within one line */ 2050 { 2051 if (u_save_cursor() == FAIL) /* save line for undo */ 2052 return FAIL; 2053 2054 /* if 'cpoptions' contains '$', display '$' at end of change */ 2055 if ( vim_strchr(p_cpo, CPO_DOLLAR) != NULL 2056 && oap->op_type == OP_CHANGE 2057 && oap->end.lnum == curwin->w_cursor.lnum 2058 && !oap->is_VIsual) 2059 display_dollar(oap->end.col - !oap->inclusive); 2060 2061 n = oap->end.col - oap->start.col + 1 - !oap->inclusive; 2062 2063 #ifdef FEAT_VIRTUALEDIT 2064 if (virtual_op) 2065 { 2066 /* fix up things for virtualedit-delete: 2067 * break the tabs which are going to get in our way 2068 */ 2069 char_u *curline = ml_get_curline(); 2070 int len = (int)STRLEN(curline); 2071 2072 if (oap->end.coladd != 0 2073 && (int)oap->end.col >= len - 1 2074 && !(oap->start.coladd && (int)oap->end.col >= len - 1)) 2075 n++; 2076 /* Delete at least one char (e.g, when on a control char). */ 2077 if (n == 0 && oap->start.coladd != oap->end.coladd) 2078 n = 1; 2079 2080 /* When deleted a char in the line, reset coladd. */ 2081 if (gchar_cursor() != NUL) 2082 curwin->w_cursor.coladd = 0; 2083 } 2084 #endif 2085 (void)del_bytes((long)n, !virtual_op, 2086 oap->op_type == OP_DELETE && !oap->is_VIsual); 2087 } 2088 else /* delete characters between lines */ 2089 { 2090 pos_T curpos; 2091 2092 /* save deleted and changed lines for undo */ 2093 if (u_save((linenr_T)(curwin->w_cursor.lnum - 1), 2094 (linenr_T)(curwin->w_cursor.lnum + oap->line_count)) == FAIL) 2095 return FAIL; 2096 2097 truncate_line(TRUE); /* delete from cursor to end of line */ 2098 2099 curpos = curwin->w_cursor; /* remember curwin->w_cursor */ 2100 ++curwin->w_cursor.lnum; 2101 del_lines((long)(oap->line_count - 2), FALSE); 2102 2103 /* delete from start of line until op_end */ 2104 n = (oap->end.col + 1 - !oap->inclusive); 2105 curwin->w_cursor.col = 0; 2106 (void)del_bytes((long)n, !virtual_op, 2107 oap->op_type == OP_DELETE && !oap->is_VIsual); 2108 curwin->w_cursor = curpos; /* restore curwin->w_cursor */ 2109 (void)do_join(2, FALSE, FALSE, FALSE, FALSE); 2110 } 2111 } 2112 2113 msgmore(curbuf->b_ml.ml_line_count - old_lcount); 2114 2115 #ifdef FEAT_VIRTUALEDIT 2116 setmarks: 2117 #endif 2118 if (oap->block_mode) 2119 { 2120 curbuf->b_op_end.lnum = oap->end.lnum; 2121 curbuf->b_op_end.col = oap->start.col; 2122 } 2123 else 2124 curbuf->b_op_end = oap->start; 2125 curbuf->b_op_start = oap->start; 2126 2127 return OK; 2128 } 2129 2130 #ifdef FEAT_MBYTE 2131 /* 2132 * Adjust end of operating area for ending on a multi-byte character. 2133 * Used for deletion. 2134 */ 2135 static void 2136 mb_adjust_opend(oparg_T *oap) 2137 { 2138 char_u *p; 2139 2140 if (oap->inclusive) 2141 { 2142 p = ml_get(oap->end.lnum); 2143 oap->end.col += mb_tail_off(p, p + oap->end.col); 2144 } 2145 } 2146 #endif 2147 2148 #if defined(FEAT_VISUALEXTRA) || defined(PROTO) 2149 2150 # ifdef FEAT_MBYTE 2151 /* 2152 * Replace the character under the cursor with "c". 2153 * This takes care of multi-byte characters. 2154 */ 2155 static void 2156 replace_character(int c) 2157 { 2158 int n = State; 2159 2160 State = REPLACE; 2161 ins_char(c); 2162 State = n; 2163 /* Backup to the replaced character. */ 2164 dec_cursor(); 2165 } 2166 2167 # endif 2168 /* 2169 * Replace a whole area with one character. 2170 */ 2171 int 2172 op_replace(oparg_T *oap, int c) 2173 { 2174 int n, numc; 2175 #ifdef FEAT_MBYTE 2176 int num_chars; 2177 #endif 2178 char_u *newp, *oldp; 2179 size_t oldlen; 2180 struct block_def bd; 2181 char_u *after_p = NULL; 2182 int had_ctrl_v_cr = FALSE; 2183 2184 if ((curbuf->b_ml.ml_flags & ML_EMPTY ) || oap->empty) 2185 return OK; /* nothing to do */ 2186 2187 if (c == REPLACE_CR_NCHAR) 2188 { 2189 had_ctrl_v_cr = TRUE; 2190 c = CAR; 2191 } 2192 else if (c == REPLACE_NL_NCHAR) 2193 { 2194 had_ctrl_v_cr = TRUE; 2195 c = NL; 2196 } 2197 2198 #ifdef FEAT_MBYTE 2199 if (has_mbyte) 2200 mb_adjust_opend(oap); 2201 #endif 2202 2203 if (u_save((linenr_T)(oap->start.lnum - 1), 2204 (linenr_T)(oap->end.lnum + 1)) == FAIL) 2205 return FAIL; 2206 2207 /* 2208 * block mode replace 2209 */ 2210 if (oap->block_mode) 2211 { 2212 bd.is_MAX = (curwin->w_curswant == MAXCOL); 2213 for ( ; curwin->w_cursor.lnum <= oap->end.lnum; ++curwin->w_cursor.lnum) 2214 { 2215 curwin->w_cursor.col = 0; /* make sure cursor position is valid */ 2216 block_prep(oap, &bd, curwin->w_cursor.lnum, TRUE); 2217 if (bd.textlen == 0 && (!virtual_op || bd.is_MAX)) 2218 continue; /* nothing to replace */ 2219 2220 /* n == number of extra chars required 2221 * If we split a TAB, it may be replaced by several characters. 2222 * Thus the number of characters may increase! 2223 */ 2224 #ifdef FEAT_VIRTUALEDIT 2225 /* If the range starts in virtual space, count the initial 2226 * coladd offset as part of "startspaces" */ 2227 if (virtual_op && bd.is_short && *bd.textstart == NUL) 2228 { 2229 pos_T vpos; 2230 2231 vpos.lnum = curwin->w_cursor.lnum; 2232 getvpos(&vpos, oap->start_vcol); 2233 bd.startspaces += vpos.coladd; 2234 n = bd.startspaces; 2235 } 2236 else 2237 #endif 2238 /* allow for pre spaces */ 2239 n = (bd.startspaces ? bd.start_char_vcols - 1 : 0); 2240 2241 /* allow for post spp */ 2242 n += (bd.endspaces 2243 #ifdef FEAT_VIRTUALEDIT 2244 && !bd.is_oneChar 2245 #endif 2246 && bd.end_char_vcols > 0) ? bd.end_char_vcols - 1 : 0; 2247 /* Figure out how many characters to replace. */ 2248 numc = oap->end_vcol - oap->start_vcol + 1; 2249 if (bd.is_short && (!virtual_op || bd.is_MAX)) 2250 numc -= (oap->end_vcol - bd.end_vcol) + 1; 2251 2252 #ifdef FEAT_MBYTE 2253 /* A double-wide character can be replaced only up to half the 2254 * times. */ 2255 if ((*mb_char2cells)(c) > 1) 2256 { 2257 if ((numc & 1) && !bd.is_short) 2258 { 2259 ++bd.endspaces; 2260 ++n; 2261 } 2262 numc = numc / 2; 2263 } 2264 2265 /* Compute bytes needed, move character count to num_chars. */ 2266 num_chars = numc; 2267 numc *= (*mb_char2len)(c); 2268 #endif 2269 /* oldlen includes textlen, so don't double count */ 2270 n += numc - bd.textlen; 2271 2272 oldp = ml_get_curline(); 2273 oldlen = STRLEN(oldp); 2274 newp = alloc_check((unsigned)oldlen + 1 + n); 2275 if (newp == NULL) 2276 continue; 2277 vim_memset(newp, NUL, (size_t)(oldlen + 1 + n)); 2278 /* copy up to deleted part */ 2279 mch_memmove(newp, oldp, (size_t)bd.textcol); 2280 oldp += bd.textcol + bd.textlen; 2281 /* insert pre-spaces */ 2282 vim_memset(newp + bd.textcol, ' ', (size_t)bd.startspaces); 2283 /* insert replacement chars CHECK FOR ALLOCATED SPACE */ 2284 /* REPLACE_CR_NCHAR/REPLACE_NL_NCHAR is used for entering CR 2285 * literally. */ 2286 if (had_ctrl_v_cr || (c != '\r' && c != '\n')) 2287 { 2288 #ifdef FEAT_MBYTE 2289 if (has_mbyte) 2290 { 2291 n = (int)STRLEN(newp); 2292 while (--num_chars >= 0) 2293 n += (*mb_char2bytes)(c, newp + n); 2294 } 2295 else 2296 #endif 2297 vim_memset(newp + STRLEN(newp), c, (size_t)numc); 2298 if (!bd.is_short) 2299 { 2300 /* insert post-spaces */ 2301 vim_memset(newp + STRLEN(newp), ' ', (size_t)bd.endspaces); 2302 /* copy the part after the changed part */ 2303 STRMOVE(newp + STRLEN(newp), oldp); 2304 } 2305 } 2306 else 2307 { 2308 /* Replacing with \r or \n means splitting the line. */ 2309 after_p = alloc_check( 2310 (unsigned)(oldlen + 1 + n - STRLEN(newp))); 2311 if (after_p != NULL) 2312 STRMOVE(after_p, oldp); 2313 } 2314 /* replace the line */ 2315 ml_replace(curwin->w_cursor.lnum, newp, FALSE); 2316 if (after_p != NULL) 2317 { 2318 ml_append(curwin->w_cursor.lnum++, after_p, 0, FALSE); 2319 appended_lines_mark(curwin->w_cursor.lnum, 1L); 2320 oap->end.lnum++; 2321 vim_free(after_p); 2322 } 2323 } 2324 } 2325 else 2326 { 2327 /* 2328 * MCHAR and MLINE motion replace. 2329 */ 2330 if (oap->motion_type == MLINE) 2331 { 2332 oap->start.col = 0; 2333 curwin->w_cursor.col = 0; 2334 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); 2335 if (oap->end.col) 2336 --oap->end.col; 2337 } 2338 else if (!oap->inclusive) 2339 dec(&(oap->end)); 2340 2341 while (LTOREQ_POS(curwin->w_cursor, oap->end)) 2342 { 2343 n = gchar_cursor(); 2344 if (n != NUL) 2345 { 2346 #ifdef FEAT_MBYTE 2347 if ((*mb_char2len)(c) > 1 || (*mb_char2len)(n) > 1) 2348 { 2349 /* This is slow, but it handles replacing a single-byte 2350 * with a multi-byte and the other way around. */ 2351 if (curwin->w_cursor.lnum == oap->end.lnum) 2352 oap->end.col += (*mb_char2len)(c) - (*mb_char2len)(n); 2353 replace_character(c); 2354 } 2355 else 2356 #endif 2357 { 2358 #ifdef FEAT_VIRTUALEDIT 2359 if (n == TAB) 2360 { 2361 int end_vcol = 0; 2362 2363 if (curwin->w_cursor.lnum == oap->end.lnum) 2364 { 2365 /* oap->end has to be recalculated when 2366 * the tab breaks */ 2367 end_vcol = getviscol2(oap->end.col, 2368 oap->end.coladd); 2369 } 2370 coladvance_force(getviscol()); 2371 if (curwin->w_cursor.lnum == oap->end.lnum) 2372 getvpos(&oap->end, end_vcol); 2373 } 2374 #endif 2375 PBYTE(curwin->w_cursor, c); 2376 } 2377 } 2378 #ifdef FEAT_VIRTUALEDIT 2379 else if (virtual_op && curwin->w_cursor.lnum == oap->end.lnum) 2380 { 2381 int virtcols = oap->end.coladd; 2382 2383 if (curwin->w_cursor.lnum == oap->start.lnum 2384 && oap->start.col == oap->end.col && oap->start.coladd) 2385 virtcols -= oap->start.coladd; 2386 2387 /* oap->end has been trimmed so it's effectively inclusive; 2388 * as a result an extra +1 must be counted so we don't 2389 * trample the NUL byte. */ 2390 coladvance_force(getviscol2(oap->end.col, oap->end.coladd) + 1); 2391 curwin->w_cursor.col -= (virtcols + 1); 2392 for (; virtcols >= 0; virtcols--) 2393 { 2394 #ifdef FEAT_MBYTE 2395 if ((*mb_char2len)(c) > 1) 2396 replace_character(c); 2397 else 2398 #endif 2399 PBYTE(curwin->w_cursor, c); 2400 if (inc(&curwin->w_cursor) == -1) 2401 break; 2402 } 2403 } 2404 #endif 2405 2406 /* Advance to next character, stop at the end of the file. */ 2407 if (inc_cursor() == -1) 2408 break; 2409 } 2410 } 2411 2412 curwin->w_cursor = oap->start; 2413 check_cursor(); 2414 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L); 2415 2416 /* Set "'[" and "']" marks. */ 2417 curbuf->b_op_start = oap->start; 2418 curbuf->b_op_end = oap->end; 2419 2420 return OK; 2421 } 2422 #endif 2423 2424 static int swapchars(int op_type, pos_T *pos, int length); 2425 2426 /* 2427 * Handle the (non-standard vi) tilde operator. Also for "gu", "gU" and "g?". 2428 */ 2429 void 2430 op_tilde(oparg_T *oap) 2431 { 2432 pos_T pos; 2433 struct block_def bd; 2434 int did_change = FALSE; 2435 2436 if (u_save((linenr_T)(oap->start.lnum - 1), 2437 (linenr_T)(oap->end.lnum + 1)) == FAIL) 2438 return; 2439 2440 pos = oap->start; 2441 if (oap->block_mode) /* Visual block mode */ 2442 { 2443 for (; pos.lnum <= oap->end.lnum; ++pos.lnum) 2444 { 2445 int one_change; 2446 2447 block_prep(oap, &bd, pos.lnum, FALSE); 2448 pos.col = bd.textcol; 2449 one_change = swapchars(oap->op_type, &pos, bd.textlen); 2450 did_change |= one_change; 2451 2452 #ifdef FEAT_NETBEANS_INTG 2453 if (netbeans_active() && one_change) 2454 { 2455 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); 2456 2457 netbeans_removed(curbuf, pos.lnum, bd.textcol, 2458 (long)bd.textlen); 2459 netbeans_inserted(curbuf, pos.lnum, bd.textcol, 2460 &ptr[bd.textcol], bd.textlen); 2461 } 2462 #endif 2463 } 2464 if (did_change) 2465 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); 2466 } 2467 else /* not block mode */ 2468 { 2469 if (oap->motion_type == MLINE) 2470 { 2471 oap->start.col = 0; 2472 pos.col = 0; 2473 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); 2474 if (oap->end.col) 2475 --oap->end.col; 2476 } 2477 else if (!oap->inclusive) 2478 dec(&(oap->end)); 2479 2480 if (pos.lnum == oap->end.lnum) 2481 did_change = swapchars(oap->op_type, &pos, 2482 oap->end.col - pos.col + 1); 2483 else 2484 for (;;) 2485 { 2486 did_change |= swapchars(oap->op_type, &pos, 2487 pos.lnum == oap->end.lnum ? oap->end.col + 1: 2488 (int)STRLEN(ml_get_pos(&pos))); 2489 if (LTOREQ_POS(oap->end, pos) || inc(&pos) == -1) 2490 break; 2491 } 2492 if (did_change) 2493 { 2494 changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 2495 0L); 2496 #ifdef FEAT_NETBEANS_INTG 2497 if (netbeans_active() && did_change) 2498 { 2499 char_u *ptr; 2500 int count; 2501 2502 pos = oap->start; 2503 while (pos.lnum < oap->end.lnum) 2504 { 2505 ptr = ml_get_buf(curbuf, pos.lnum, FALSE); 2506 count = (int)STRLEN(ptr) - pos.col; 2507 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); 2508 netbeans_inserted(curbuf, pos.lnum, pos.col, 2509 &ptr[pos.col], count); 2510 pos.col = 0; 2511 pos.lnum++; 2512 } 2513 ptr = ml_get_buf(curbuf, pos.lnum, FALSE); 2514 count = oap->end.col - pos.col + 1; 2515 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count); 2516 netbeans_inserted(curbuf, pos.lnum, pos.col, 2517 &ptr[pos.col], count); 2518 } 2519 #endif 2520 } 2521 } 2522 2523 if (!did_change && oap->is_VIsual) 2524 /* No change: need to remove the Visual selection */ 2525 redraw_curbuf_later(INVERTED); 2526 2527 /* 2528 * Set '[ and '] marks. 2529 */ 2530 curbuf->b_op_start = oap->start; 2531 curbuf->b_op_end = oap->end; 2532 2533 if (oap->line_count > p_report) 2534 { 2535 if (oap->line_count == 1) 2536 MSG(_("1 line changed")); 2537 else 2538 smsg((char_u *)_("%ld lines changed"), oap->line_count); 2539 } 2540 } 2541 2542 /* 2543 * Invoke swapchar() on "length" bytes at position "pos". 2544 * "pos" is advanced to just after the changed characters. 2545 * "length" is rounded up to include the whole last multi-byte character. 2546 * Also works correctly when the number of bytes changes. 2547 * Returns TRUE if some character was changed. 2548 */ 2549 static int 2550 swapchars(int op_type, pos_T *pos, int length) 2551 { 2552 int todo; 2553 int did_change = 0; 2554 2555 for (todo = length; todo > 0; --todo) 2556 { 2557 # ifdef FEAT_MBYTE 2558 if (has_mbyte) 2559 { 2560 int len = (*mb_ptr2len)(ml_get_pos(pos)); 2561 2562 /* we're counting bytes, not characters */ 2563 if (len > 0) 2564 todo -= len - 1; 2565 } 2566 # endif 2567 did_change |= swapchar(op_type, pos); 2568 if (inc(pos) == -1) /* at end of file */ 2569 break; 2570 } 2571 return did_change; 2572 } 2573 2574 /* 2575 * If op_type == OP_UPPER: make uppercase, 2576 * if op_type == OP_LOWER: make lowercase, 2577 * if op_type == OP_ROT13: do rot13 encoding, 2578 * else swap case of character at 'pos' 2579 * returns TRUE when something actually changed. 2580 */ 2581 int 2582 swapchar(int op_type, pos_T *pos) 2583 { 2584 int c; 2585 int nc; 2586 2587 c = gchar_pos(pos); 2588 2589 /* Only do rot13 encoding for ASCII characters. */ 2590 if (c >= 0x80 && op_type == OP_ROT13) 2591 return FALSE; 2592 2593 #ifdef FEAT_MBYTE 2594 if (op_type == OP_UPPER && c == 0xdf 2595 && (enc_latin1like || STRCMP(p_enc, "iso-8859-2") == 0)) 2596 { 2597 pos_T sp = curwin->w_cursor; 2598 2599 /* Special handling of German sharp s: change to "SS". */ 2600 curwin->w_cursor = *pos; 2601 del_char(FALSE); 2602 ins_char('S'); 2603 ins_char('S'); 2604 curwin->w_cursor = sp; 2605 inc(pos); 2606 } 2607 2608 if (enc_dbcs != 0 && c >= 0x100) /* No lower/uppercase letter */ 2609 return FALSE; 2610 #endif 2611 nc = c; 2612 if (MB_ISLOWER(c)) 2613 { 2614 if (op_type == OP_ROT13) 2615 nc = ROT13(c, 'a'); 2616 else if (op_type != OP_LOWER) 2617 nc = MB_TOUPPER(c); 2618 } 2619 else if (MB_ISUPPER(c)) 2620 { 2621 if (op_type == OP_ROT13) 2622 nc = ROT13(c, 'A'); 2623 else if (op_type != OP_UPPER) 2624 nc = MB_TOLOWER(c); 2625 } 2626 if (nc != c) 2627 { 2628 #ifdef FEAT_MBYTE 2629 if (enc_utf8 && (c >= 0x80 || nc >= 0x80)) 2630 { 2631 pos_T sp = curwin->w_cursor; 2632 2633 curwin->w_cursor = *pos; 2634 /* don't use del_char(), it also removes composing chars */ 2635 del_bytes(utf_ptr2len(ml_get_cursor()), FALSE, FALSE); 2636 ins_char(nc); 2637 curwin->w_cursor = sp; 2638 } 2639 else 2640 #endif 2641 PBYTE(*pos, nc); 2642 return TRUE; 2643 } 2644 return FALSE; 2645 } 2646 2647 #if defined(FEAT_VISUALEXTRA) || defined(PROTO) 2648 /* 2649 * op_insert - Insert and append operators for Visual mode. 2650 */ 2651 void 2652 op_insert(oparg_T *oap, long count1) 2653 { 2654 long ins_len, pre_textlen = 0; 2655 char_u *firstline, *ins_text; 2656 colnr_T ind_pre = 0, ind_post; 2657 struct block_def bd; 2658 int i; 2659 pos_T t1; 2660 2661 /* edit() changes this - record it for OP_APPEND */ 2662 bd.is_MAX = (curwin->w_curswant == MAXCOL); 2663 2664 /* vis block is still marked. Get rid of it now. */ 2665 curwin->w_cursor.lnum = oap->start.lnum; 2666 update_screen(INVERTED); 2667 2668 if (oap->block_mode) 2669 { 2670 #ifdef FEAT_VIRTUALEDIT 2671 /* When 'virtualedit' is used, need to insert the extra spaces before 2672 * doing block_prep(). When only "block" is used, virtual edit is 2673 * already disabled, but still need it when calling 2674 * coladvance_force(). */ 2675 if (curwin->w_cursor.coladd > 0) 2676 { 2677 int old_ve_flags = ve_flags; 2678 2679 ve_flags = VE_ALL; 2680 if (u_save_cursor() == FAIL) 2681 return; 2682 coladvance_force(oap->op_type == OP_APPEND 2683 ? oap->end_vcol + 1 : getviscol()); 2684 if (oap->op_type == OP_APPEND) 2685 --curwin->w_cursor.col; 2686 ve_flags = old_ve_flags; 2687 } 2688 #endif 2689 /* Get the info about the block before entering the text */ 2690 block_prep(oap, &bd, oap->start.lnum, TRUE); 2691 /* Get indent information */ 2692 ind_pre = (colnr_T)getwhitecols_curline(); 2693 firstline = ml_get(oap->start.lnum) + bd.textcol; 2694 2695 if (oap->op_type == OP_APPEND) 2696 firstline += bd.textlen; 2697 pre_textlen = (long)STRLEN(firstline); 2698 } 2699 2700 if (oap->op_type == OP_APPEND) 2701 { 2702 if (oap->block_mode 2703 #ifdef FEAT_VIRTUALEDIT 2704 && curwin->w_cursor.coladd == 0 2705 #endif 2706 ) 2707 { 2708 /* Move the cursor to the character right of the block. */ 2709 curwin->w_set_curswant = TRUE; 2710 while (*ml_get_cursor() != NUL 2711 && (curwin->w_cursor.col < bd.textcol + bd.textlen)) 2712 ++curwin->w_cursor.col; 2713 if (bd.is_short && !bd.is_MAX) 2714 { 2715 /* First line was too short, make it longer and adjust the 2716 * values in "bd". */ 2717 if (u_save_cursor() == FAIL) 2718 return; 2719 for (i = 0; i < bd.endspaces; ++i) 2720 ins_char(' '); 2721 bd.textlen += bd.endspaces; 2722 } 2723 } 2724 else 2725 { 2726 curwin->w_cursor = oap->end; 2727 check_cursor_col(); 2728 2729 /* Works just like an 'i'nsert on the next character. */ 2730 if (!LINEEMPTY(curwin->w_cursor.lnum) 2731 && oap->start_vcol != oap->end_vcol) 2732 inc_cursor(); 2733 } 2734 } 2735 2736 t1 = oap->start; 2737 (void)edit(NUL, FALSE, (linenr_T)count1); 2738 2739 /* When a tab was inserted, and the characters in front of the tab 2740 * have been converted to a tab as well, the column of the cursor 2741 * might have actually been reduced, so need to adjust here. */ 2742 if (t1.lnum == curbuf->b_op_start_orig.lnum 2743 && LT_POS(curbuf->b_op_start_orig, t1)) 2744 oap->start = curbuf->b_op_start_orig; 2745 2746 /* If user has moved off this line, we don't know what to do, so do 2747 * nothing. 2748 * Also don't repeat the insert when Insert mode ended with CTRL-C. */ 2749 if (curwin->w_cursor.lnum != oap->start.lnum || got_int) 2750 return; 2751 2752 if (oap->block_mode) 2753 { 2754 struct block_def bd2; 2755 int did_indent = FALSE; 2756 size_t len; 2757 int add; 2758 2759 /* If indent kicked in, the firstline might have changed 2760 * but only do that, if the indent actually increased. */ 2761 ind_post = (colnr_T)getwhitecols_curline(); 2762 if (curbuf->b_op_start.col > ind_pre && ind_post > ind_pre) 2763 { 2764 bd.textcol += ind_post - ind_pre; 2765 bd.start_vcol += ind_post - ind_pre; 2766 did_indent = TRUE; 2767 } 2768 2769 /* The user may have moved the cursor before inserting something, try 2770 * to adjust the block for that. But only do it, if the difference 2771 * does not come from indent kicking in. */ 2772 if (oap->start.lnum == curbuf->b_op_start_orig.lnum 2773 && !bd.is_MAX && !did_indent) 2774 { 2775 if (oap->op_type == OP_INSERT 2776 && oap->start.col 2777 #ifdef FEAT_VIRTUALEDIT 2778 + oap->start.coladd 2779 #endif 2780 != curbuf->b_op_start_orig.col 2781 #ifdef FEAT_VIRTUALEDIT 2782 + curbuf->b_op_start_orig.coladd 2783 #endif 2784 ) 2785 { 2786 int t = getviscol2(curbuf->b_op_start_orig.col, 2787 curbuf->b_op_start_orig.coladd); 2788 oap->start.col = curbuf->b_op_start_orig.col; 2789 pre_textlen -= t - oap->start_vcol; 2790 oap->start_vcol = t; 2791 } 2792 else if (oap->op_type == OP_APPEND 2793 && oap->end.col 2794 #ifdef FEAT_VIRTUALEDIT 2795 + oap->end.coladd 2796 #endif 2797 >= curbuf->b_op_start_orig.col 2798 #ifdef FEAT_VIRTUALEDIT 2799 + curbuf->b_op_start_orig.coladd 2800 #endif 2801 ) 2802 { 2803 int t = getviscol2(curbuf->b_op_start_orig.col, 2804 curbuf->b_op_start_orig.coladd); 2805 oap->start.col = curbuf->b_op_start_orig.col; 2806 /* reset pre_textlen to the value of OP_INSERT */ 2807 pre_textlen += bd.textlen; 2808 pre_textlen -= t - oap->start_vcol; 2809 oap->start_vcol = t; 2810 oap->op_type = OP_INSERT; 2811 } 2812 } 2813 2814 /* 2815 * Spaces and tabs in the indent may have changed to other spaces and 2816 * tabs. Get the starting column again and correct the length. 2817 * Don't do this when "$" used, end-of-line will have changed. 2818 */ 2819 block_prep(oap, &bd2, oap->start.lnum, TRUE); 2820 if (!bd.is_MAX || bd2.textlen < bd.textlen) 2821 { 2822 if (oap->op_type == OP_APPEND) 2823 { 2824 pre_textlen += bd2.textlen - bd.textlen; 2825 if (bd2.endspaces) 2826 --bd2.textlen; 2827 } 2828 bd.textcol = bd2.textcol; 2829 bd.textlen = bd2.textlen; 2830 } 2831 2832 /* 2833 * Subsequent calls to ml_get() flush the firstline data - take a 2834 * copy of the required string. 2835 */ 2836 firstline = ml_get(oap->start.lnum); 2837 len = STRLEN(firstline); 2838 add = bd.textcol; 2839 if (oap->op_type == OP_APPEND) 2840 add += bd.textlen; 2841 if ((size_t)add > len) 2842 firstline += len; // short line, point to the NUL 2843 else 2844 firstline += add; 2845 if (pre_textlen >= 0 2846 && (ins_len = (long)STRLEN(firstline) - pre_textlen) > 0) 2847 { 2848 ins_text = vim_strnsave(firstline, (int)ins_len); 2849 if (ins_text != NULL) 2850 { 2851 /* block handled here */ 2852 if (u_save(oap->start.lnum, 2853 (linenr_T)(oap->end.lnum + 1)) == OK) 2854 block_insert(oap, ins_text, (oap->op_type == OP_INSERT), 2855 &bd); 2856 2857 curwin->w_cursor.col = oap->start.col; 2858 check_cursor(); 2859 vim_free(ins_text); 2860 } 2861 } 2862 } 2863 } 2864 #endif 2865 2866 /* 2867 * op_change - handle a change operation 2868 * 2869 * return TRUE if edit() returns because of a CTRL-O command 2870 */ 2871 int 2872 op_change(oparg_T *oap) 2873 { 2874 colnr_T l; 2875 int retval; 2876 #ifdef FEAT_VISUALEXTRA 2877 long offset; 2878 linenr_T linenr; 2879 long ins_len; 2880 long pre_textlen = 0; 2881 long pre_indent = 0; 2882 char_u *firstline; 2883 char_u *ins_text, *newp, *oldp; 2884 struct block_def bd; 2885 #endif 2886 2887 l = oap->start.col; 2888 if (oap->motion_type == MLINE) 2889 { 2890 l = 0; 2891 #ifdef FEAT_SMARTINDENT 2892 if (!p_paste && curbuf->b_p_si 2893 # ifdef FEAT_CINDENT 2894 && !curbuf->b_p_cin 2895 # endif 2896 ) 2897 can_si = TRUE; /* It's like opening a new line, do si */ 2898 #endif 2899 } 2900 2901 /* First delete the text in the region. In an empty buffer only need to 2902 * save for undo */ 2903 if (curbuf->b_ml.ml_flags & ML_EMPTY) 2904 { 2905 if (u_save_cursor() == FAIL) 2906 return FALSE; 2907 } 2908 else if (op_delete(oap) == FAIL) 2909 return FALSE; 2910 2911 if ((l > curwin->w_cursor.col) && !LINEEMPTY(curwin->w_cursor.lnum) 2912 && !virtual_op) 2913 inc_cursor(); 2914 2915 #ifdef FEAT_VISUALEXTRA 2916 /* check for still on same line (<CR> in inserted text meaningless) */ 2917 /* skip blank lines too */ 2918 if (oap->block_mode) 2919 { 2920 # ifdef FEAT_VIRTUALEDIT 2921 /* Add spaces before getting the current line length. */ 2922 if (virtual_op && (curwin->w_cursor.coladd > 0 2923 || gchar_cursor() == NUL)) 2924 coladvance_force(getviscol()); 2925 # endif 2926 firstline = ml_get(oap->start.lnum); 2927 pre_textlen = (long)STRLEN(firstline); 2928 pre_indent = (long)getwhitecols(firstline); 2929 bd.textcol = curwin->w_cursor.col; 2930 } 2931 #endif 2932 2933 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) 2934 if (oap->motion_type == MLINE) 2935 fix_indent(); 2936 #endif 2937 2938 retval = edit(NUL, FALSE, (linenr_T)1); 2939 2940 #ifdef FEAT_VISUALEXTRA 2941 /* 2942 * In Visual block mode, handle copying the new text to all lines of the 2943 * block. 2944 * Don't repeat the insert when Insert mode ended with CTRL-C. 2945 */ 2946 if (oap->block_mode && oap->start.lnum != oap->end.lnum && !got_int) 2947 { 2948 /* Auto-indenting may have changed the indent. If the cursor was past 2949 * the indent, exclude that indent change from the inserted text. */ 2950 firstline = ml_get(oap->start.lnum); 2951 if (bd.textcol > (colnr_T)pre_indent) 2952 { 2953 long new_indent = (long)getwhitecols(firstline); 2954 2955 pre_textlen += new_indent - pre_indent; 2956 bd.textcol += new_indent - pre_indent; 2957 } 2958 2959 ins_len = (long)STRLEN(firstline) - pre_textlen; 2960 if (ins_len > 0) 2961 { 2962 /* Subsequent calls to ml_get() flush the firstline data - take a 2963 * copy of the inserted text. */ 2964 if ((ins_text = alloc_check((unsigned)(ins_len + 1))) != NULL) 2965 { 2966 vim_strncpy(ins_text, firstline + bd.textcol, (size_t)ins_len); 2967 for (linenr = oap->start.lnum + 1; linenr <= oap->end.lnum; 2968 linenr++) 2969 { 2970 block_prep(oap, &bd, linenr, TRUE); 2971 if (!bd.is_short || virtual_op) 2972 { 2973 # ifdef FEAT_VIRTUALEDIT 2974 pos_T vpos; 2975 2976 /* If the block starts in virtual space, count the 2977 * initial coladd offset as part of "startspaces" */ 2978 if (bd.is_short) 2979 { 2980 vpos.lnum = linenr; 2981 (void)getvpos(&vpos, oap->start_vcol); 2982 } 2983 else 2984 vpos.coladd = 0; 2985 # endif 2986 oldp = ml_get(linenr); 2987 newp = alloc_check((unsigned)(STRLEN(oldp) 2988 # ifdef FEAT_VIRTUALEDIT 2989 + vpos.coladd 2990 # endif 2991 + ins_len + 1)); 2992 if (newp == NULL) 2993 continue; 2994 /* copy up to block start */ 2995 mch_memmove(newp, oldp, (size_t)bd.textcol); 2996 offset = bd.textcol; 2997 # ifdef FEAT_VIRTUALEDIT 2998 vim_memset(newp + offset, ' ', (size_t)vpos.coladd); 2999 offset += vpos.coladd; 3000 # endif 3001 mch_memmove(newp + offset, ins_text, (size_t)ins_len); 3002 offset += ins_len; 3003 oldp += bd.textcol; 3004 STRMOVE(newp + offset, oldp); 3005 ml_replace(linenr, newp, FALSE); 3006 } 3007 } 3008 check_cursor(); 3009 3010 changed_lines(oap->start.lnum + 1, 0, oap->end.lnum + 1, 0L); 3011 } 3012 vim_free(ins_text); 3013 } 3014 } 3015 #endif 3016 3017 return retval; 3018 } 3019 3020 /* 3021 * set all the yank registers to empty (called from main()) 3022 */ 3023 void 3024 init_yank(void) 3025 { 3026 int i; 3027 3028 for (i = 0; i < NUM_REGISTERS; ++i) 3029 y_regs[i].y_array = NULL; 3030 } 3031 3032 #if defined(EXITFREE) || defined(PROTO) 3033 void 3034 clear_registers(void) 3035 { 3036 int i; 3037 3038 for (i = 0; i < NUM_REGISTERS; ++i) 3039 { 3040 y_current = &y_regs[i]; 3041 if (y_current->y_array != NULL) 3042 free_yank_all(); 3043 } 3044 } 3045 #endif 3046 3047 /* 3048 * Free "n" lines from the current yank register. 3049 * Called for normal freeing and in case of error. 3050 */ 3051 static void 3052 free_yank(long n) 3053 { 3054 if (y_current->y_array != NULL) 3055 { 3056 long i; 3057 3058 for (i = n; --i >= 0; ) 3059 { 3060 #ifdef AMIGA /* only for very slow machines */ 3061 if ((i & 1023) == 1023) /* this may take a while */ 3062 { 3063 /* 3064 * This message should never cause a hit-return message. 3065 * Overwrite this message with any next message. 3066 */ 3067 ++no_wait_return; 3068 smsg((char_u *)_("freeing %ld lines"), i + 1); 3069 --no_wait_return; 3070 msg_didout = FALSE; 3071 msg_col = 0; 3072 } 3073 #endif 3074 vim_free(y_current->y_array[i]); 3075 } 3076 VIM_CLEAR(y_current->y_array); 3077 #ifdef AMIGA 3078 if (n >= 1000) 3079 MSG(""); 3080 #endif 3081 } 3082 } 3083 3084 static void 3085 free_yank_all(void) 3086 { 3087 free_yank(y_current->y_size); 3088 } 3089 3090 /* 3091 * Yank the text between "oap->start" and "oap->end" into a yank register. 3092 * If we are to append (uppercase register), we first yank into a new yank 3093 * register and then concatenate the old and the new one (so we keep the old 3094 * one in case of out-of-memory). 3095 * 3096 * Return FAIL for failure, OK otherwise. 3097 */ 3098 int 3099 op_yank(oparg_T *oap, int deleting, int mess) 3100 { 3101 long y_idx; /* index in y_array[] */ 3102 yankreg_T *curr; /* copy of y_current */ 3103 yankreg_T newreg; /* new yank register when appending */ 3104 char_u **new_ptr; 3105 linenr_T lnum; /* current line number */ 3106 long j; 3107 int yanktype = oap->motion_type; 3108 long yanklines = oap->line_count; 3109 linenr_T yankendlnum = oap->end.lnum; 3110 char_u *p; 3111 char_u *pnew; 3112 struct block_def bd; 3113 #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) 3114 int did_star = FALSE; 3115 #endif 3116 3117 /* check for read-only register */ 3118 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE)) 3119 { 3120 beep_flush(); 3121 return FAIL; 3122 } 3123 if (oap->regname == '_') /* black hole: nothing to do */ 3124 return OK; 3125 3126 #ifdef FEAT_CLIPBOARD 3127 if (!clip_star.available && oap->regname == '*') 3128 oap->regname = 0; 3129 else if (!clip_plus.available && oap->regname == '+') 3130 oap->regname = 0; 3131 #endif 3132 3133 if (!deleting) /* op_delete() already set y_current */ 3134 get_yank_register(oap->regname, TRUE); 3135 3136 curr = y_current; 3137 /* append to existing contents */ 3138 if (y_append && y_current->y_array != NULL) 3139 y_current = &newreg; 3140 else 3141 free_yank_all(); /* free previously yanked lines */ 3142 3143 /* 3144 * If the cursor was in column 1 before and after the movement, and the 3145 * operator is not inclusive, the yank is always linewise. 3146 */ 3147 if ( oap->motion_type == MCHAR 3148 && oap->start.col == 0 3149 && !oap->inclusive 3150 && (!oap->is_VIsual || *p_sel == 'o') 3151 && !oap->block_mode 3152 && oap->end.col == 0 3153 && yanklines > 1) 3154 { 3155 yanktype = MLINE; 3156 --yankendlnum; 3157 --yanklines; 3158 } 3159 3160 y_current->y_size = yanklines; 3161 y_current->y_type = yanktype; /* set the yank register type */ 3162 y_current->y_width = 0; 3163 y_current->y_array = (char_u **)lalloc_clear((long_u)(sizeof(char_u *) * 3164 yanklines), TRUE); 3165 if (y_current->y_array == NULL) 3166 { 3167 y_current = curr; 3168 return FAIL; 3169 } 3170 #ifdef FEAT_VIMINFO 3171 y_current->y_time_set = vim_time(); 3172 #endif 3173 3174 y_idx = 0; 3175 lnum = oap->start.lnum; 3176 3177 if (oap->block_mode) 3178 { 3179 /* Visual block mode */ 3180 y_current->y_type = MBLOCK; /* set the yank register type */ 3181 y_current->y_width = oap->end_vcol - oap->start_vcol; 3182 3183 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0) 3184 y_current->y_width--; 3185 } 3186 3187 for ( ; lnum <= yankendlnum; lnum++, y_idx++) 3188 { 3189 switch (y_current->y_type) 3190 { 3191 case MBLOCK: 3192 block_prep(oap, &bd, lnum, FALSE); 3193 if (yank_copy_line(&bd, y_idx) == FAIL) 3194 goto fail; 3195 break; 3196 3197 case MLINE: 3198 if ((y_current->y_array[y_idx] = 3199 vim_strsave(ml_get(lnum))) == NULL) 3200 goto fail; 3201 break; 3202 3203 case MCHAR: 3204 { 3205 colnr_T startcol = 0, endcol = MAXCOL; 3206 #ifdef FEAT_VIRTUALEDIT 3207 int is_oneChar = FALSE; 3208 colnr_T cs, ce; 3209 #endif 3210 p = ml_get(lnum); 3211 bd.startspaces = 0; 3212 bd.endspaces = 0; 3213 3214 if (lnum == oap->start.lnum) 3215 { 3216 startcol = oap->start.col; 3217 #ifdef FEAT_VIRTUALEDIT 3218 if (virtual_op) 3219 { 3220 getvcol(curwin, &oap->start, &cs, NULL, &ce); 3221 if (ce != cs && oap->start.coladd > 0) 3222 { 3223 /* Part of a tab selected -- but don't 3224 * double-count it. */ 3225 bd.startspaces = (ce - cs + 1) 3226 - oap->start.coladd; 3227 startcol++; 3228 } 3229 } 3230 #endif 3231 } 3232 3233 if (lnum == oap->end.lnum) 3234 { 3235 endcol = oap->end.col; 3236 #ifdef FEAT_VIRTUALEDIT 3237 if (virtual_op) 3238 { 3239 getvcol(curwin, &oap->end, &cs, NULL, &ce); 3240 if (p[endcol] == NUL || (cs + oap->end.coladd < ce 3241 # ifdef FEAT_MBYTE 3242 /* Don't add space for double-wide 3243 * char; endcol will be on last byte 3244 * of multi-byte char. */ 3245 && (*mb_head_off)(p, p + endcol) == 0 3246 # endif 3247 )) 3248 { 3249 if (oap->start.lnum == oap->end.lnum 3250 && oap->start.col == oap->end.col) 3251 { 3252 /* Special case: inside a single char */ 3253 is_oneChar = TRUE; 3254 bd.startspaces = oap->end.coladd 3255 - oap->start.coladd + oap->inclusive; 3256 endcol = startcol; 3257 } 3258 else 3259 { 3260 bd.endspaces = oap->end.coladd 3261 + oap->inclusive; 3262 endcol -= oap->inclusive; 3263 } 3264 } 3265 } 3266 #endif 3267 } 3268 if (endcol == MAXCOL) 3269 endcol = (colnr_T)STRLEN(p); 3270 if (startcol > endcol 3271 #ifdef FEAT_VIRTUALEDIT 3272 || is_oneChar 3273 #endif 3274 ) 3275 bd.textlen = 0; 3276 else 3277 { 3278 bd.textlen = endcol - startcol + oap->inclusive; 3279 } 3280 bd.textstart = p + startcol; 3281 if (yank_copy_line(&bd, y_idx) == FAIL) 3282 goto fail; 3283 break; 3284 } 3285 /* NOTREACHED */ 3286 } 3287 } 3288 3289 if (curr != y_current) /* append the new block to the old block */ 3290 { 3291 new_ptr = (char_u **)lalloc((long_u)(sizeof(char_u *) * 3292 (curr->y_size + y_current->y_size)), TRUE); 3293 if (new_ptr == NULL) 3294 goto fail; 3295 for (j = 0; j < curr->y_size; ++j) 3296 new_ptr[j] = curr->y_array[j]; 3297 vim_free(curr->y_array); 3298 curr->y_array = new_ptr; 3299 #ifdef FEAT_VIMINFO 3300 curr->y_time_set = vim_time(); 3301 #endif 3302 3303 if (yanktype == MLINE) /* MLINE overrides MCHAR and MBLOCK */ 3304 curr->y_type = MLINE; 3305 3306 /* Concatenate the last line of the old block with the first line of 3307 * the new block, unless being Vi compatible. */ 3308 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL) 3309 { 3310 pnew = lalloc((long_u)(STRLEN(curr->y_array[curr->y_size - 1]) 3311 + STRLEN(y_current->y_array[0]) + 1), TRUE); 3312 if (pnew == NULL) 3313 { 3314 y_idx = y_current->y_size - 1; 3315 goto fail; 3316 } 3317 STRCPY(pnew, curr->y_array[--j]); 3318 STRCAT(pnew, y_current->y_array[0]); 3319 vim_free(curr->y_array[j]); 3320 vim_free(y_current->y_array[0]); 3321 curr->y_array[j++] = pnew; 3322 y_idx = 1; 3323 } 3324 else 3325 y_idx = 0; 3326 while (y_idx < y_current->y_size) 3327 curr->y_array[j++] = y_current->y_array[y_idx++]; 3328 curr->y_size = j; 3329 vim_free(y_current->y_array); 3330 y_current = curr; 3331 } 3332 if (curwin->w_p_rnu) 3333 redraw_later(SOME_VALID); /* cursor moved to start */ 3334 if (mess) /* Display message about yank? */ 3335 { 3336 if (yanktype == MCHAR 3337 && !oap->block_mode 3338 && yanklines == 1) 3339 yanklines = 0; 3340 /* Some versions of Vi use ">=" here, some don't... */ 3341 if (yanklines > p_report) 3342 { 3343 char namebuf[100]; 3344 3345 if (oap->regname == NUL) 3346 *namebuf = NUL; 3347 else 3348 vim_snprintf(namebuf, sizeof(namebuf), 3349 _(" into \"%c"), oap->regname); 3350 3351 /* redisplay now, so message is not deleted */ 3352 update_topline_redraw(); 3353 if (yanklines == 1) 3354 { 3355 if (oap->block_mode) 3356 smsg((char_u *)_("block of 1 line yanked%s"), namebuf); 3357 else 3358 smsg((char_u *)_("1 line yanked%s"), namebuf); 3359 } 3360 else if (oap->block_mode) 3361 smsg((char_u *)_("block of %ld lines yanked%s"), 3362 yanklines, namebuf); 3363 else 3364 smsg((char_u *)_("%ld lines yanked%s"), yanklines, 3365 namebuf); 3366 } 3367 } 3368 3369 /* 3370 * Set "'[" and "']" marks. 3371 */ 3372 curbuf->b_op_start = oap->start; 3373 curbuf->b_op_end = oap->end; 3374 if (yanktype == MLINE && !oap->block_mode) 3375 { 3376 curbuf->b_op_start.col = 0; 3377 curbuf->b_op_end.col = MAXCOL; 3378 } 3379 3380 #ifdef FEAT_CLIPBOARD 3381 /* 3382 * If we were yanking to the '*' register, send result to clipboard. 3383 * If no register was specified, and "unnamed" in 'clipboard', make a copy 3384 * to the '*' register. 3385 */ 3386 if (clip_star.available 3387 && (curr == &(y_regs[STAR_REGISTER]) 3388 || (!deleting && oap->regname == 0 3389 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED)))) 3390 { 3391 if (curr != &(y_regs[STAR_REGISTER])) 3392 /* Copy the text from register 0 to the clipboard register. */ 3393 copy_yank_reg(&(y_regs[STAR_REGISTER])); 3394 3395 clip_own_selection(&clip_star); 3396 clip_gen_set_selection(&clip_star); 3397 # ifdef FEAT_X11 3398 did_star = TRUE; 3399 # endif 3400 } 3401 3402 # ifdef FEAT_X11 3403 /* 3404 * If we were yanking to the '+' register, send result to selection. 3405 * Also copy to the '*' register, in case auto-select is off. 3406 */ 3407 if (clip_plus.available 3408 && (curr == &(y_regs[PLUS_REGISTER]) 3409 || (!deleting && oap->regname == 0 3410 && ((clip_unnamed | clip_unnamed_saved) & 3411 CLIP_UNNAMED_PLUS)))) 3412 { 3413 if (curr != &(y_regs[PLUS_REGISTER])) 3414 /* Copy the text from register 0 to the clipboard register. */ 3415 copy_yank_reg(&(y_regs[PLUS_REGISTER])); 3416 3417 clip_own_selection(&clip_plus); 3418 clip_gen_set_selection(&clip_plus); 3419 if (!clip_isautosel_star() && !clip_isautosel_plus() 3420 && !did_star && curr == &(y_regs[PLUS_REGISTER])) 3421 { 3422 copy_yank_reg(&(y_regs[STAR_REGISTER])); 3423 clip_own_selection(&clip_star); 3424 clip_gen_set_selection(&clip_star); 3425 } 3426 } 3427 # endif 3428 #endif 3429 3430 #if defined(FEAT_EVAL) 3431 if (!deleting && has_textyankpost()) 3432 yank_do_autocmd(oap, y_current); 3433 #endif 3434 3435 return OK; 3436 3437 fail: /* free the allocated lines */ 3438 free_yank(y_idx + 1); 3439 y_current = curr; 3440 return FAIL; 3441 } 3442 3443 static int 3444 yank_copy_line(struct block_def *bd, long y_idx) 3445 { 3446 char_u *pnew; 3447 3448 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1)) 3449 == NULL) 3450 return FAIL; 3451 y_current->y_array[y_idx] = pnew; 3452 vim_memset(pnew, ' ', (size_t)bd->startspaces); 3453 pnew += bd->startspaces; 3454 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen); 3455 pnew += bd->textlen; 3456 vim_memset(pnew, ' ', (size_t)bd->endspaces); 3457 pnew += bd->endspaces; 3458 *pnew = NUL; 3459 return OK; 3460 } 3461 3462 #ifdef FEAT_CLIPBOARD 3463 /* 3464 * Make a copy of the y_current register to register "reg". 3465 */ 3466 static void 3467 copy_yank_reg(yankreg_T *reg) 3468 { 3469 yankreg_T *curr = y_current; 3470 long j; 3471 3472 y_current = reg; 3473 free_yank_all(); 3474 *y_current = *curr; 3475 y_current->y_array = (char_u **)lalloc_clear( 3476 (long_u)(sizeof(char_u *) * y_current->y_size), TRUE); 3477 if (y_current->y_array == NULL) 3478 y_current->y_size = 0; 3479 else 3480 for (j = 0; j < y_current->y_size; ++j) 3481 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL) 3482 { 3483 free_yank(j); 3484 y_current->y_size = 0; 3485 break; 3486 } 3487 y_current = curr; 3488 } 3489 #endif 3490 3491 /* 3492 * Put contents of register "regname" into the text. 3493 * Caller must check "regname" to be valid! 3494 * "flags": PUT_FIXINDENT make indent look nice 3495 * PUT_CURSEND leave cursor after end of new text 3496 * PUT_LINE force linewise put (":put") 3497 */ 3498 void 3499 do_put( 3500 int regname, 3501 int dir, /* BACKWARD for 'P', FORWARD for 'p' */ 3502 long count, 3503 int flags) 3504 { 3505 char_u *ptr; 3506 char_u *newp, *oldp; 3507 int yanklen; 3508 int totlen = 0; /* init for gcc */ 3509 linenr_T lnum; 3510 colnr_T col; 3511 long i; /* index in y_array[] */ 3512 int y_type; 3513 long y_size; 3514 int oldlen; 3515 long y_width = 0; 3516 colnr_T vcol; 3517 int delcount; 3518 int incr = 0; 3519 long j; 3520 struct block_def bd; 3521 char_u **y_array = NULL; 3522 long nr_lines = 0; 3523 pos_T new_cursor; 3524 int indent; 3525 int orig_indent = 0; /* init for gcc */ 3526 int indent_diff = 0; /* init for gcc */ 3527 int first_indent = TRUE; 3528 int lendiff = 0; 3529 pos_T old_pos; 3530 char_u *insert_string = NULL; 3531 int allocated = FALSE; 3532 long cnt; 3533 3534 #ifdef FEAT_CLIPBOARD 3535 /* Adjust register name for "unnamed" in 'clipboard'. */ 3536 adjust_clip_reg(®name); 3537 (void)may_get_selection(regname); 3538 #endif 3539 3540 if (flags & PUT_FIXINDENT) 3541 orig_indent = get_indent(); 3542 3543 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ 3544 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ 3545 3546 /* 3547 * Using inserted text works differently, because the register includes 3548 * special characters (newlines, etc.). 3549 */ 3550 if (regname == '.') 3551 { 3552 if (VIsual_active) 3553 stuffcharReadbuff(VIsual_mode); 3554 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') : 3555 (count == -1 ? 'O' : 'i')), count, FALSE); 3556 /* Putting the text is done later, so can't really move the cursor to 3557 * the next character. Use "l" to simulate it. */ 3558 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL) 3559 stuffcharReadbuff('l'); 3560 return; 3561 } 3562 3563 /* 3564 * For special registers '%' (file name), '#' (alternate file name) and 3565 * ':' (last command line), etc. we have to create a fake yank register. 3566 */ 3567 if (get_spec_reg(regname, &insert_string, &allocated, TRUE)) 3568 { 3569 if (insert_string == NULL) 3570 return; 3571 } 3572 3573 /* Autocommands may be executed when saving lines for undo. This might 3574 * make "y_array" invalid, so we start undo now to avoid that. */ 3575 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL) 3576 goto end; 3577 3578 if (insert_string != NULL) 3579 { 3580 y_type = MCHAR; 3581 #ifdef FEAT_EVAL 3582 if (regname == '=') 3583 { 3584 /* For the = register we need to split the string at NL 3585 * characters. 3586 * Loop twice: count the number of lines and save them. */ 3587 for (;;) 3588 { 3589 y_size = 0; 3590 ptr = insert_string; 3591 while (ptr != NULL) 3592 { 3593 if (y_array != NULL) 3594 y_array[y_size] = ptr; 3595 ++y_size; 3596 ptr = vim_strchr(ptr, '\n'); 3597 if (ptr != NULL) 3598 { 3599 if (y_array != NULL) 3600 *ptr = NUL; 3601 ++ptr; 3602 /* A trailing '\n' makes the register linewise. */ 3603 if (*ptr == NUL) 3604 { 3605 y_type = MLINE; 3606 break; 3607 } 3608 } 3609 } 3610 if (y_array != NULL) 3611 break; 3612 y_array = (char_u **)alloc((unsigned) 3613 (y_size * sizeof(char_u *))); 3614 if (y_array == NULL) 3615 goto end; 3616 } 3617 } 3618 else 3619 #endif 3620 { 3621 y_size = 1; /* use fake one-line yank register */ 3622 y_array = &insert_string; 3623 } 3624 } 3625 else 3626 { 3627 get_yank_register(regname, FALSE); 3628 3629 y_type = y_current->y_type; 3630 y_width = y_current->y_width; 3631 y_size = y_current->y_size; 3632 y_array = y_current->y_array; 3633 } 3634 3635 if (y_type == MLINE) 3636 { 3637 if (flags & PUT_LINE_SPLIT) 3638 { 3639 char_u *p; 3640 3641 /* "p" or "P" in Visual mode: split the lines to put the text in 3642 * between. */ 3643 if (u_save_cursor() == FAIL) 3644 goto end; 3645 p = ml_get_cursor(); 3646 if (dir == FORWARD && *p != NUL) 3647 MB_PTR_ADV(p); 3648 ptr = vim_strsave(p); 3649 if (ptr == NULL) 3650 goto end; 3651 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE); 3652 vim_free(ptr); 3653 3654 oldp = ml_get_curline(); 3655 p = oldp + curwin->w_cursor.col; 3656 if (dir == FORWARD && *p != NUL) 3657 MB_PTR_ADV(p); 3658 ptr = vim_strnsave(oldp, p - oldp); 3659 if (ptr == NULL) 3660 goto end; 3661 ml_replace(curwin->w_cursor.lnum, ptr, FALSE); 3662 ++nr_lines; 3663 dir = FORWARD; 3664 } 3665 if (flags & PUT_LINE_FORWARD) 3666 { 3667 /* Must be "p" for a Visual block, put lines below the block. */ 3668 curwin->w_cursor = curbuf->b_visual.vi_end; 3669 dir = FORWARD; 3670 } 3671 curbuf->b_op_start = curwin->w_cursor; /* default for '[ mark */ 3672 curbuf->b_op_end = curwin->w_cursor; /* default for '] mark */ 3673 } 3674 3675 if (flags & PUT_LINE) /* :put command or "p" in Visual line mode. */ 3676 y_type = MLINE; 3677 3678 if (y_size == 0 || y_array == NULL) 3679 { 3680 EMSG2(_("E353: Nothing in register %s"), 3681 regname == 0 ? (char_u *)"\"" : transchar(regname)); 3682 goto end; 3683 } 3684 3685 if (y_type == MBLOCK) 3686 { 3687 lnum = curwin->w_cursor.lnum + y_size + 1; 3688 if (lnum > curbuf->b_ml.ml_line_count) 3689 lnum = curbuf->b_ml.ml_line_count + 1; 3690 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) 3691 goto end; 3692 } 3693 else if (y_type == MLINE) 3694 { 3695 lnum = curwin->w_cursor.lnum; 3696 #ifdef FEAT_FOLDING 3697 /* Correct line number for closed fold. Don't move the cursor yet, 3698 * u_save() uses it. */ 3699 if (dir == BACKWARD) 3700 (void)hasFolding(lnum, &lnum, NULL); 3701 else 3702 (void)hasFolding(lnum, NULL, &lnum); 3703 #endif 3704 if (dir == FORWARD) 3705 ++lnum; 3706 /* In an empty buffer the empty line is going to be replaced, include 3707 * it in the saved lines. */ 3708 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) 3709 goto end; 3710 #ifdef FEAT_FOLDING 3711 if (dir == FORWARD) 3712 curwin->w_cursor.lnum = lnum - 1; 3713 else 3714 curwin->w_cursor.lnum = lnum; 3715 curbuf->b_op_start = curwin->w_cursor; /* for mark_adjust() */ 3716 #endif 3717 } 3718 else if (u_save_cursor() == FAIL) 3719 goto end; 3720 3721 yanklen = (int)STRLEN(y_array[0]); 3722 3723 #ifdef FEAT_VIRTUALEDIT 3724 if (ve_flags == VE_ALL && y_type == MCHAR) 3725 { 3726 if (gchar_cursor() == TAB) 3727 { 3728 /* Don't need to insert spaces when "p" on the last position of a 3729 * tab or "P" on the first position. */ 3730 #ifdef FEAT_VARTABS 3731 int viscol = getviscol(); 3732 if (dir == FORWARD 3733 ? tabstop_padding(viscol, curbuf->b_p_ts, 3734 curbuf->b_p_vts_array) != 1 3735 : curwin->w_cursor.coladd > 0) 3736 coladvance_force(viscol); 3737 #else 3738 if (dir == FORWARD 3739 ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1 3740 : curwin->w_cursor.coladd > 0) 3741 coladvance_force(getviscol()); 3742 #endif 3743 else 3744 curwin->w_cursor.coladd = 0; 3745 } 3746 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL) 3747 coladvance_force(getviscol() + (dir == FORWARD)); 3748 } 3749 #endif 3750 3751 lnum = curwin->w_cursor.lnum; 3752 col = curwin->w_cursor.col; 3753 3754 /* 3755 * Block mode 3756 */ 3757 if (y_type == MBLOCK) 3758 { 3759 int c = gchar_cursor(); 3760 colnr_T endcol2 = 0; 3761 3762 if (dir == FORWARD && c != NUL) 3763 { 3764 #ifdef FEAT_VIRTUALEDIT 3765 if (ve_flags == VE_ALL) 3766 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); 3767 else 3768 #endif 3769 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); 3770 3771 #ifdef FEAT_MBYTE 3772 if (has_mbyte) 3773 /* move to start of next multi-byte character */ 3774 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); 3775 else 3776 #endif 3777 #ifdef FEAT_VIRTUALEDIT 3778 if (c != TAB || ve_flags != VE_ALL) 3779 #endif 3780 ++curwin->w_cursor.col; 3781 ++col; 3782 } 3783 else 3784 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); 3785 3786 #ifdef FEAT_VIRTUALEDIT 3787 col += curwin->w_cursor.coladd; 3788 if (ve_flags == VE_ALL 3789 && (curwin->w_cursor.coladd > 0 3790 || endcol2 == curwin->w_cursor.col)) 3791 { 3792 if (dir == FORWARD && c == NUL) 3793 ++col; 3794 if (dir != FORWARD && c != NUL) 3795 ++curwin->w_cursor.col; 3796 if (c == TAB) 3797 { 3798 if (dir == BACKWARD && curwin->w_cursor.col) 3799 curwin->w_cursor.col--; 3800 if (dir == FORWARD && col - 1 == endcol2) 3801 curwin->w_cursor.col++; 3802 } 3803 } 3804 curwin->w_cursor.coladd = 0; 3805 #endif 3806 bd.textcol = 0; 3807 for (i = 0; i < y_size; ++i) 3808 { 3809 int spaces; 3810 char shortline; 3811 3812 bd.startspaces = 0; 3813 bd.endspaces = 0; 3814 vcol = 0; 3815 delcount = 0; 3816 3817 /* add a new line */ 3818 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) 3819 { 3820 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", 3821 (colnr_T)1, FALSE) == FAIL) 3822 break; 3823 ++nr_lines; 3824 } 3825 /* get the old line and advance to the position to insert at */ 3826 oldp = ml_get_curline(); 3827 oldlen = (int)STRLEN(oldp); 3828 for (ptr = oldp; vcol < col && *ptr; ) 3829 { 3830 /* Count a tab for what it's worth (if list mode not on) */ 3831 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol); 3832 vcol += incr; 3833 } 3834 bd.textcol = (colnr_T)(ptr - oldp); 3835 3836 shortline = (vcol < col) || (vcol == col && !*ptr) ; 3837 3838 if (vcol < col) /* line too short, padd with spaces */ 3839 bd.startspaces = col - vcol; 3840 else if (vcol > col) 3841 { 3842 bd.endspaces = vcol - col; 3843 bd.startspaces = incr - bd.endspaces; 3844 --bd.textcol; 3845 delcount = 1; 3846 #ifdef FEAT_MBYTE 3847 if (has_mbyte) 3848 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol); 3849 #endif 3850 if (oldp[bd.textcol] != TAB) 3851 { 3852 /* Only a Tab can be split into spaces. Other 3853 * characters will have to be moved to after the 3854 * block, causing misalignment. */ 3855 delcount = 0; 3856 bd.endspaces = 0; 3857 } 3858 } 3859 3860 yanklen = (int)STRLEN(y_array[i]); 3861 3862 /* calculate number of spaces required to fill right side of block*/ 3863 spaces = y_width + 1; 3864 for (j = 0; j < yanklen; j++) 3865 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0); 3866 if (spaces < 0) 3867 spaces = 0; 3868 3869 /* insert the new text */ 3870 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; 3871 newp = alloc_check((unsigned)totlen + oldlen + 1); 3872 if (newp == NULL) 3873 break; 3874 /* copy part up to cursor to new line */ 3875 ptr = newp; 3876 mch_memmove(ptr, oldp, (size_t)bd.textcol); 3877 ptr += bd.textcol; 3878 /* may insert some spaces before the new text */ 3879 vim_memset(ptr, ' ', (size_t)bd.startspaces); 3880 ptr += bd.startspaces; 3881 /* insert the new text */ 3882 for (j = 0; j < count; ++j) 3883 { 3884 mch_memmove(ptr, y_array[i], (size_t)yanklen); 3885 ptr += yanklen; 3886 3887 /* insert block's trailing spaces only if there's text behind */ 3888 if ((j < count - 1 || !shortline) && spaces) 3889 { 3890 vim_memset(ptr, ' ', (size_t)spaces); 3891 ptr += spaces; 3892 } 3893 } 3894 /* may insert some spaces after the new text */ 3895 vim_memset(ptr, ' ', (size_t)bd.endspaces); 3896 ptr += bd.endspaces; 3897 /* move the text after the cursor to the end of the line. */ 3898 mch_memmove(ptr, oldp + bd.textcol + delcount, 3899 (size_t)(oldlen - bd.textcol - delcount + 1)); 3900 ml_replace(curwin->w_cursor.lnum, newp, FALSE); 3901 3902 ++curwin->w_cursor.lnum; 3903 if (i == 0) 3904 curwin->w_cursor.col += bd.startspaces; 3905 } 3906 3907 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines); 3908 3909 /* Set '[ mark. */ 3910 curbuf->b_op_start = curwin->w_cursor; 3911 curbuf->b_op_start.lnum = lnum; 3912 3913 /* adjust '] mark */ 3914 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; 3915 curbuf->b_op_end.col = bd.textcol + totlen - 1; 3916 # ifdef FEAT_VIRTUALEDIT 3917 curbuf->b_op_end.coladd = 0; 3918 # endif 3919 if (flags & PUT_CURSEND) 3920 { 3921 colnr_T len; 3922 3923 curwin->w_cursor = curbuf->b_op_end; 3924 curwin->w_cursor.col++; 3925 3926 /* in Insert mode we might be after the NUL, correct for that */ 3927 len = (colnr_T)STRLEN(ml_get_curline()); 3928 if (curwin->w_cursor.col > len) 3929 curwin->w_cursor.col = len; 3930 } 3931 else 3932 curwin->w_cursor.lnum = lnum; 3933 } 3934 else 3935 { 3936 /* 3937 * Character or Line mode 3938 */ 3939 if (y_type == MCHAR) 3940 { 3941 /* if type is MCHAR, FORWARD is the same as BACKWARD on the next 3942 * char */ 3943 if (dir == FORWARD && gchar_cursor() != NUL) 3944 { 3945 #ifdef FEAT_MBYTE 3946 if (has_mbyte) 3947 { 3948 int bytelen = (*mb_ptr2len)(ml_get_cursor()); 3949 3950 /* put it on the next of the multi-byte character. */ 3951 col += bytelen; 3952 if (yanklen) 3953 { 3954 curwin->w_cursor.col += bytelen; 3955 curbuf->b_op_end.col += bytelen; 3956 } 3957 } 3958 else 3959 #endif 3960 { 3961 ++col; 3962 if (yanklen) 3963 { 3964 ++curwin->w_cursor.col; 3965 ++curbuf->b_op_end.col; 3966 } 3967 } 3968 } 3969 curbuf->b_op_start = curwin->w_cursor; 3970 } 3971 /* 3972 * Line mode: BACKWARD is the same as FORWARD on the previous line 3973 */ 3974 else if (dir == BACKWARD) 3975 --lnum; 3976 new_cursor = curwin->w_cursor; 3977 3978 /* 3979 * simple case: insert into current line 3980 */ 3981 if (y_type == MCHAR && y_size == 1) 3982 { 3983 linenr_T end_lnum = 0; /* init for gcc */ 3984 3985 if (VIsual_active) 3986 { 3987 end_lnum = curbuf->b_visual.vi_end.lnum; 3988 if (end_lnum < curbuf->b_visual.vi_start.lnum) 3989 end_lnum = curbuf->b_visual.vi_start.lnum; 3990 } 3991 3992 do { 3993 totlen = count * yanklen; 3994 if (totlen > 0) 3995 { 3996 oldp = ml_get(lnum); 3997 if (VIsual_active && col > (int)STRLEN(oldp)) 3998 { 3999 lnum++; 4000 continue; 4001 } 4002 newp = alloc_check((unsigned)(STRLEN(oldp) + totlen + 1)); 4003 if (newp == NULL) 4004 goto end; /* alloc() gave an error message */ 4005 mch_memmove(newp, oldp, (size_t)col); 4006 ptr = newp + col; 4007 for (i = 0; i < count; ++i) 4008 { 4009 mch_memmove(ptr, y_array[0], (size_t)yanklen); 4010 ptr += yanklen; 4011 } 4012 STRMOVE(ptr, oldp + col); 4013 ml_replace(lnum, newp, FALSE); 4014 /* Place cursor on last putted char. */ 4015 if (lnum == curwin->w_cursor.lnum) 4016 { 4017 /* make sure curwin->w_virtcol is updated */ 4018 changed_cline_bef_curs(); 4019 curwin->w_cursor.col += (colnr_T)(totlen - 1); 4020 } 4021 } 4022 if (VIsual_active) 4023 lnum++; 4024 } while (VIsual_active && lnum <= end_lnum); 4025 4026 if (VIsual_active) /* reset lnum to the last visual line */ 4027 lnum--; 4028 4029 curbuf->b_op_end = curwin->w_cursor; 4030 /* For "CTRL-O p" in Insert mode, put cursor after last char */ 4031 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND))) 4032 ++curwin->w_cursor.col; 4033 changed_bytes(lnum, col); 4034 } 4035 else 4036 { 4037 /* 4038 * Insert at least one line. When y_type is MCHAR, break the first 4039 * line in two. 4040 */ 4041 for (cnt = 1; cnt <= count; ++cnt) 4042 { 4043 i = 0; 4044 if (y_type == MCHAR) 4045 { 4046 /* 4047 * Split the current line in two at the insert position. 4048 * First insert y_array[size - 1] in front of second line. 4049 * Then append y_array[0] to first line. 4050 */ 4051 lnum = new_cursor.lnum; 4052 ptr = ml_get(lnum) + col; 4053 totlen = (int)STRLEN(y_array[y_size - 1]); 4054 newp = alloc_check((unsigned)(STRLEN(ptr) + totlen + 1)); 4055 if (newp == NULL) 4056 goto error; 4057 STRCPY(newp, y_array[y_size - 1]); 4058 STRCAT(newp, ptr); 4059 /* insert second line */ 4060 ml_append(lnum, newp, (colnr_T)0, FALSE); 4061 vim_free(newp); 4062 4063 oldp = ml_get(lnum); 4064 newp = alloc_check((unsigned)(col + yanklen + 1)); 4065 if (newp == NULL) 4066 goto error; 4067 /* copy first part of line */ 4068 mch_memmove(newp, oldp, (size_t)col); 4069 /* append to first line */ 4070 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1)); 4071 ml_replace(lnum, newp, FALSE); 4072 4073 curwin->w_cursor.lnum = lnum; 4074 i = 1; 4075 } 4076 4077 for (; i < y_size; ++i) 4078 { 4079 if ((y_type != MCHAR || i < y_size - 1) 4080 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE) 4081 == FAIL) 4082 goto error; 4083 lnum++; 4084 ++nr_lines; 4085 if (flags & PUT_FIXINDENT) 4086 { 4087 old_pos = curwin->w_cursor; 4088 curwin->w_cursor.lnum = lnum; 4089 ptr = ml_get(lnum); 4090 if (cnt == count && i == y_size - 1) 4091 lendiff = (int)STRLEN(ptr); 4092 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) 4093 if (*ptr == '#' && preprocs_left()) 4094 indent = 0; /* Leave # lines at start */ 4095 else 4096 #endif 4097 if (*ptr == NUL) 4098 indent = 0; /* Ignore empty lines */ 4099 else if (first_indent) 4100 { 4101 indent_diff = orig_indent - get_indent(); 4102 indent = orig_indent; 4103 first_indent = FALSE; 4104 } 4105 else if ((indent = get_indent() + indent_diff) < 0) 4106 indent = 0; 4107 (void)set_indent(indent, 0); 4108 curwin->w_cursor = old_pos; 4109 /* remember how many chars were removed */ 4110 if (cnt == count && i == y_size - 1) 4111 lendiff -= (int)STRLEN(ml_get(lnum)); 4112 } 4113 } 4114 } 4115 4116 error: 4117 /* Adjust marks. */ 4118 if (y_type == MLINE) 4119 { 4120 curbuf->b_op_start.col = 0; 4121 if (dir == FORWARD) 4122 curbuf->b_op_start.lnum++; 4123 } 4124 /* Skip mark_adjust when adding lines after the last one, there 4125 * can't be marks there. But still needed in diff mode. */ 4126 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines 4127 < curbuf->b_ml.ml_line_count 4128 #ifdef FEAT_DIFF 4129 || curwin->w_p_diff 4130 #endif 4131 ) 4132 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR), 4133 (linenr_T)MAXLNUM, nr_lines, 0L); 4134 4135 /* note changed text for displaying and folding */ 4136 if (y_type == MCHAR) 4137 changed_lines(curwin->w_cursor.lnum, col, 4138 curwin->w_cursor.lnum + 1, nr_lines); 4139 else 4140 changed_lines(curbuf->b_op_start.lnum, 0, 4141 curbuf->b_op_start.lnum, nr_lines); 4142 4143 /* put '] mark at last inserted character */ 4144 curbuf->b_op_end.lnum = lnum; 4145 /* correct length for change in indent */ 4146 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; 4147 if (col > 1) 4148 curbuf->b_op_end.col = col - 1; 4149 else 4150 curbuf->b_op_end.col = 0; 4151 4152 if (flags & PUT_CURSLINE) 4153 { 4154 /* ":put": put cursor on last inserted line */ 4155 curwin->w_cursor.lnum = lnum; 4156 beginline(BL_WHITE | BL_FIX); 4157 } 4158 else if (flags & PUT_CURSEND) 4159 { 4160 /* put cursor after inserted text */ 4161 if (y_type == MLINE) 4162 { 4163 if (lnum >= curbuf->b_ml.ml_line_count) 4164 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 4165 else 4166 curwin->w_cursor.lnum = lnum + 1; 4167 curwin->w_cursor.col = 0; 4168 } 4169 else 4170 { 4171 curwin->w_cursor.lnum = lnum; 4172 curwin->w_cursor.col = col; 4173 } 4174 } 4175 else if (y_type == MLINE) 4176 { 4177 /* put cursor on first non-blank in first inserted line */ 4178 curwin->w_cursor.col = 0; 4179 if (dir == FORWARD) 4180 ++curwin->w_cursor.lnum; 4181 beginline(BL_WHITE | BL_FIX); 4182 } 4183 else /* put cursor on first inserted character */ 4184 curwin->w_cursor = new_cursor; 4185 } 4186 } 4187 4188 msgmore(nr_lines); 4189 curwin->w_set_curswant = TRUE; 4190 4191 end: 4192 if (allocated) 4193 vim_free(insert_string); 4194 if (regname == '=') 4195 vim_free(y_array); 4196 4197 VIsual_active = FALSE; 4198 4199 /* If the cursor is past the end of the line put it at the end. */ 4200 adjust_cursor_eol(); 4201 } 4202 4203 /* 4204 * When the cursor is on the NUL past the end of the line and it should not be 4205 * there move it left. 4206 */ 4207 void 4208 adjust_cursor_eol(void) 4209 { 4210 if (curwin->w_cursor.col > 0 4211 && gchar_cursor() == NUL 4212 #ifdef FEAT_VIRTUALEDIT 4213 && (ve_flags & VE_ONEMORE) == 0 4214 #endif 4215 && !(restart_edit || (State & INSERT))) 4216 { 4217 /* Put the cursor on the last character in the line. */ 4218 dec_cursor(); 4219 4220 #ifdef FEAT_VIRTUALEDIT 4221 if (ve_flags == VE_ALL) 4222 { 4223 colnr_T scol, ecol; 4224 4225 /* Coladd is set to the width of the last character. */ 4226 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol); 4227 curwin->w_cursor.coladd = ecol - scol + 1; 4228 } 4229 #endif 4230 } 4231 } 4232 4233 #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) || defined(PROTO) 4234 /* 4235 * Return TRUE if lines starting with '#' should be left aligned. 4236 */ 4237 int 4238 preprocs_left(void) 4239 { 4240 return 4241 # ifdef FEAT_SMARTINDENT 4242 # ifdef FEAT_CINDENT 4243 (curbuf->b_p_si && !curbuf->b_p_cin) || 4244 # else 4245 curbuf->b_p_si 4246 # endif 4247 # endif 4248 # ifdef FEAT_CINDENT 4249 (curbuf->b_p_cin && in_cinkeys('#', ' ', TRUE) 4250 && curbuf->b_ind_hash_comment == 0) 4251 # endif 4252 ; 4253 } 4254 #endif 4255 4256 /* 4257 * Return the character name of the register with the given number. 4258 */ 4259 int 4260 get_register_name(int num) 4261 { 4262 if (num == -1) 4263 return '"'; 4264 else if (num < 10) 4265 return num + '0'; 4266 else if (num == DELETION_REGISTER) 4267 return '-'; 4268 #ifdef FEAT_CLIPBOARD 4269 else if (num == STAR_REGISTER) 4270 return '*'; 4271 else if (num == PLUS_REGISTER) 4272 return '+'; 4273 #endif 4274 else 4275 { 4276 #ifdef EBCDIC 4277 int i; 4278 4279 /* EBCDIC is really braindead ... */ 4280 i = 'a' + (num - 10); 4281 if (i > 'i') 4282 i += 7; 4283 if (i > 'r') 4284 i += 8; 4285 return i; 4286 #else 4287 return num + 'a' - 10; 4288 #endif 4289 } 4290 } 4291 4292 /* 4293 * ":dis" and ":registers": Display the contents of the yank registers. 4294 */ 4295 void 4296 ex_display(exarg_T *eap) 4297 { 4298 int i, n; 4299 long j; 4300 char_u *p; 4301 yankreg_T *yb; 4302 int name; 4303 int attr; 4304 char_u *arg = eap->arg; 4305 #ifdef FEAT_MBYTE 4306 int clen; 4307 #else 4308 # define clen 1 4309 #endif 4310 4311 if (arg != NULL && *arg == NUL) 4312 arg = NULL; 4313 attr = HL_ATTR(HLF_8); 4314 4315 /* Highlight title */ 4316 MSG_PUTS_TITLE(_("\n--- Registers ---")); 4317 for (i = -1; i < NUM_REGISTERS && !got_int; ++i) 4318 { 4319 name = get_register_name(i); 4320 if (arg != NULL && vim_strchr(arg, name) == NULL 4321 #ifdef ONE_CLIPBOARD 4322 /* Star register and plus register contain the same thing. */ 4323 && (name != '*' || vim_strchr(arg, '+') == NULL) 4324 #endif 4325 ) 4326 continue; /* did not ask for this register */ 4327 4328 #ifdef FEAT_CLIPBOARD 4329 /* Adjust register name for "unnamed" in 'clipboard'. 4330 * When it's a clipboard register, fill it with the current contents 4331 * of the clipboard. */ 4332 adjust_clip_reg(&name); 4333 (void)may_get_selection(name); 4334 #endif 4335 4336 if (i == -1) 4337 { 4338 if (y_previous != NULL) 4339 yb = y_previous; 4340 else 4341 yb = &(y_regs[0]); 4342 } 4343 else 4344 yb = &(y_regs[i]); 4345 4346 #ifdef FEAT_EVAL 4347 if (name == MB_TOLOWER(redir_reg) 4348 || (redir_reg == '"' && yb == y_previous)) 4349 continue; /* do not list register being written to, the 4350 * pointer can be freed */ 4351 #endif 4352 4353 if (yb->y_array != NULL) 4354 { 4355 msg_putchar('\n'); 4356 msg_putchar('"'); 4357 msg_putchar(name); 4358 MSG_PUTS(" "); 4359 4360 n = (int)Columns - 6; 4361 for (j = 0; j < yb->y_size && n > 1; ++j) 4362 { 4363 if (j) 4364 { 4365 MSG_PUTS_ATTR("^J", attr); 4366 n -= 2; 4367 } 4368 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p) 4369 { 4370 #ifdef FEAT_MBYTE 4371 clen = (*mb_ptr2len)(p); 4372 #endif 4373 msg_outtrans_len(p, clen); 4374 #ifdef FEAT_MBYTE 4375 p += clen - 1; 4376 #endif 4377 } 4378 } 4379 if (n > 1 && yb->y_type == MLINE) 4380 MSG_PUTS_ATTR("^J", attr); 4381 out_flush(); /* show one line at a time */ 4382 } 4383 ui_breakcheck(); 4384 } 4385 4386 /* 4387 * display last inserted text 4388 */ 4389 if ((p = get_last_insert()) != NULL 4390 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) 4391 { 4392 MSG_PUTS("\n\". "); 4393 dis_msg(p, TRUE); 4394 } 4395 4396 /* 4397 * display last command line 4398 */ 4399 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) 4400 && !got_int) 4401 { 4402 MSG_PUTS("\n\": "); 4403 dis_msg(last_cmdline, FALSE); 4404 } 4405 4406 /* 4407 * display current file name 4408 */ 4409 if (curbuf->b_fname != NULL 4410 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) 4411 { 4412 MSG_PUTS("\n\"% "); 4413 dis_msg(curbuf->b_fname, FALSE); 4414 } 4415 4416 /* 4417 * display alternate file name 4418 */ 4419 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) 4420 { 4421 char_u *fname; 4422 linenr_T dummy; 4423 4424 if (buflist_name_nr(0, &fname, &dummy) != FAIL) 4425 { 4426 MSG_PUTS("\n\"# "); 4427 dis_msg(fname, FALSE); 4428 } 4429 } 4430 4431 /* 4432 * display last search pattern 4433 */ 4434 if (last_search_pat() != NULL 4435 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) 4436 { 4437 MSG_PUTS("\n\"/ "); 4438 dis_msg(last_search_pat(), FALSE); 4439 } 4440 4441 #ifdef FEAT_EVAL 4442 /* 4443 * display last used expression 4444 */ 4445 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) 4446 && !got_int) 4447 { 4448 MSG_PUTS("\n\"= "); 4449 dis_msg(expr_line, FALSE); 4450 } 4451 #endif 4452 } 4453 4454 /* 4455 * display a string for do_dis() 4456 * truncate at end of screen line 4457 */ 4458 static void 4459 dis_msg( 4460 char_u *p, 4461 int skip_esc) /* if TRUE, ignore trailing ESC */ 4462 { 4463 int n; 4464 #ifdef FEAT_MBYTE 4465 int l; 4466 #endif 4467 4468 n = (int)Columns - 6; 4469 while (*p != NUL 4470 && !(*p == ESC && skip_esc && *(p + 1) == NUL) 4471 && (n -= ptr2cells(p)) >= 0) 4472 { 4473 #ifdef FEAT_MBYTE 4474 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) 4475 { 4476 msg_outtrans_len(p, l); 4477 p += l; 4478 } 4479 else 4480 #endif 4481 msg_outtrans_len(p++, 1); 4482 } 4483 ui_breakcheck(); 4484 } 4485 4486 #if defined(FEAT_COMMENTS) || defined(PROTO) 4487 /* 4488 * If "process" is TRUE and the line begins with a comment leader (possibly 4489 * after some white space), return a pointer to the text after it. Put a boolean 4490 * value indicating whether the line ends with an unclosed comment in 4491 * "is_comment". 4492 * line - line to be processed, 4493 * process - if FALSE, will only check whether the line ends with an unclosed 4494 * comment, 4495 * include_space - whether to also skip space following the comment leader, 4496 * is_comment - will indicate whether the current line ends with an unclosed 4497 * comment. 4498 */ 4499 char_u * 4500 skip_comment( 4501 char_u *line, 4502 int process, 4503 int include_space, 4504 int *is_comment) 4505 { 4506 char_u *comment_flags = NULL; 4507 int lead_len; 4508 int leader_offset = get_last_leader_offset(line, &comment_flags); 4509 4510 *is_comment = FALSE; 4511 if (leader_offset != -1) 4512 { 4513 /* Let's check whether the line ends with an unclosed comment. 4514 * If the last comment leader has COM_END in flags, there's no comment. 4515 */ 4516 while (*comment_flags) 4517 { 4518 if (*comment_flags == COM_END 4519 || *comment_flags == ':') 4520 break; 4521 ++comment_flags; 4522 } 4523 if (*comment_flags != COM_END) 4524 *is_comment = TRUE; 4525 } 4526 4527 if (process == FALSE) 4528 return line; 4529 4530 lead_len = get_leader_len(line, &comment_flags, FALSE, include_space); 4531 4532 if (lead_len == 0) 4533 return line; 4534 4535 /* Find: 4536 * - COM_END, 4537 * - colon, 4538 * whichever comes first. 4539 */ 4540 while (*comment_flags) 4541 { 4542 if (*comment_flags == COM_END 4543 || *comment_flags == ':') 4544 { 4545 break; 4546 } 4547 ++comment_flags; 4548 } 4549 4550 /* If we found a colon, it means that we are not processing a line 4551 * starting with a closing part of a three-part comment. That's good, 4552 * because we don't want to remove those as this would be annoying. 4553 */ 4554 if (*comment_flags == ':' || *comment_flags == NUL) 4555 line += lead_len; 4556 4557 return line; 4558 } 4559 #endif 4560 4561 /* 4562 * Join 'count' lines (minimal 2) at cursor position. 4563 * When "save_undo" is TRUE save lines for undo first. 4564 * Set "use_formatoptions" to FALSE when e.g. processing backspace and comment 4565 * leaders should not be removed. 4566 * When setmark is TRUE, sets the '[ and '] mark, else, the caller is expected 4567 * to set those marks. 4568 * 4569 * return FAIL for failure, OK otherwise 4570 */ 4571 int 4572 do_join( 4573 long count, 4574 int insert_space, 4575 int save_undo, 4576 int use_formatoptions UNUSED, 4577 int setmark) 4578 { 4579 char_u *curr = NULL; 4580 char_u *curr_start = NULL; 4581 char_u *cend; 4582 char_u *newp; 4583 char_u *spaces; /* number of spaces inserted before a line */ 4584 int endcurr1 = NUL; 4585 int endcurr2 = NUL; 4586 int currsize = 0; /* size of the current line */ 4587 int sumsize = 0; /* size of the long new line */ 4588 linenr_T t; 4589 colnr_T col = 0; 4590 int ret = OK; 4591 #if defined(FEAT_COMMENTS) || defined(PROTO) 4592 int *comments = NULL; 4593 int remove_comments = (use_formatoptions == TRUE) 4594 && has_format_option(FO_REMOVE_COMS); 4595 int prev_was_comment; 4596 #endif 4597 4598 4599 if (save_undo && u_save((linenr_T)(curwin->w_cursor.lnum - 1), 4600 (linenr_T)(curwin->w_cursor.lnum + count)) == FAIL) 4601 return FAIL; 4602 4603 /* Allocate an array to store the number of spaces inserted before each 4604 * line. We will use it to pre-compute the length of the new line and the 4605 * proper placement of each original line in the new one. */ 4606 spaces = lalloc_clear((long_u)count, TRUE); 4607 if (spaces == NULL) 4608 return FAIL; 4609 #if defined(FEAT_COMMENTS) || defined(PROTO) 4610 if (remove_comments) 4611 { 4612 comments = (int *)lalloc_clear((long_u)count * sizeof(int), TRUE); 4613 if (comments == NULL) 4614 { 4615 vim_free(spaces); 4616 return FAIL; 4617 } 4618 } 4619 #endif 4620 4621 /* 4622 * Don't move anything, just compute the final line length 4623 * and setup the array of space strings lengths 4624 */ 4625 for (t = 0; t < count; ++t) 4626 { 4627 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); 4628 if (t == 0 && setmark) 4629 { 4630 /* Set the '[ mark. */ 4631 curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; 4632 curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); 4633 } 4634 #if defined(FEAT_COMMENTS) || defined(PROTO) 4635 if (remove_comments) 4636 { 4637 /* We don't want to remove the comment leader if the 4638 * previous line is not a comment. */ 4639 if (t > 0 && prev_was_comment) 4640 { 4641 4642 char_u *new_curr = skip_comment(curr, TRUE, insert_space, 4643 &prev_was_comment); 4644 comments[t] = (int)(new_curr - curr); 4645 curr = new_curr; 4646 } 4647 else 4648 curr = skip_comment(curr, FALSE, insert_space, 4649 &prev_was_comment); 4650 } 4651 #endif 4652 4653 if (insert_space && t > 0) 4654 { 4655 curr = skipwhite(curr); 4656 if (*curr != ')' && currsize != 0 && endcurr1 != TAB 4657 #ifdef FEAT_MBYTE 4658 && (!has_format_option(FO_MBYTE_JOIN) 4659 || (mb_ptr2char(curr) < 0x100 && endcurr1 < 0x100)) 4660 && (!has_format_option(FO_MBYTE_JOIN2) 4661 || mb_ptr2char(curr) < 0x100 || endcurr1 < 0x100) 4662 #endif 4663 ) 4664 { 4665 /* don't add a space if the line is ending in a space */ 4666 if (endcurr1 == ' ') 4667 endcurr1 = endcurr2; 4668 else 4669 ++spaces[t]; 4670 /* extra space when 'joinspaces' set and line ends in '.' */ 4671 if ( p_js 4672 && (endcurr1 == '.' 4673 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL 4674 && (endcurr1 == '?' || endcurr1 == '!')))) 4675 ++spaces[t]; 4676 } 4677 } 4678 currsize = (int)STRLEN(curr); 4679 sumsize += currsize + spaces[t]; 4680 endcurr1 = endcurr2 = NUL; 4681 if (insert_space && currsize > 0) 4682 { 4683 #ifdef FEAT_MBYTE 4684 if (has_mbyte) 4685 { 4686 cend = curr + currsize; 4687 MB_PTR_BACK(curr, cend); 4688 endcurr1 = (*mb_ptr2char)(cend); 4689 if (cend > curr) 4690 { 4691 MB_PTR_BACK(curr, cend); 4692 endcurr2 = (*mb_ptr2char)(cend); 4693 } 4694 } 4695 else 4696 #endif 4697 { 4698 endcurr1 = *(curr + currsize - 1); 4699 if (currsize > 1) 4700 endcurr2 = *(curr + currsize - 2); 4701 } 4702 } 4703 line_breakcheck(); 4704 if (got_int) 4705 { 4706 ret = FAIL; 4707 goto theend; 4708 } 4709 } 4710 4711 /* store the column position before last line */ 4712 col = sumsize - currsize - spaces[count - 1]; 4713 4714 /* allocate the space for the new line */ 4715 newp = alloc_check((unsigned)(sumsize + 1)); 4716 cend = newp + sumsize; 4717 *cend = 0; 4718 4719 /* 4720 * Move affected lines to the new long one. 4721 * 4722 * Move marks from each deleted line to the joined line, adjusting the 4723 * column. This is not Vi compatible, but Vi deletes the marks, thus that 4724 * should not really be a problem. 4725 */ 4726 for (t = count - 1; ; --t) 4727 { 4728 cend -= currsize; 4729 mch_memmove(cend, curr, (size_t)currsize); 4730 if (spaces[t] > 0) 4731 { 4732 cend -= spaces[t]; 4733 vim_memset(cend, ' ', (size_t)(spaces[t])); 4734 } 4735 mark_col_adjust(curwin->w_cursor.lnum + t, (colnr_T)0, (linenr_T)-t, 4736 (long)(cend - newp + spaces[t] - (curr - curr_start))); 4737 if (t == 0) 4738 break; 4739 curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t - 1)); 4740 #if defined(FEAT_COMMENTS) || defined(PROTO) 4741 if (remove_comments) 4742 curr += comments[t - 1]; 4743 #endif 4744 if (insert_space && t > 1) 4745 curr = skipwhite(curr); 4746 currsize = (int)STRLEN(curr); 4747 } 4748 ml_replace(curwin->w_cursor.lnum, newp, FALSE); 4749 4750 if (setmark) 4751 { 4752 /* Set the '] mark. */ 4753 curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; 4754 curwin->w_buffer->b_op_end.col = (colnr_T)STRLEN(newp); 4755 } 4756 4757 /* Only report the change in the first line here, del_lines() will report 4758 * the deleted line. */ 4759 changed_lines(curwin->w_cursor.lnum, currsize, 4760 curwin->w_cursor.lnum + 1, 0L); 4761 4762 /* 4763 * Delete following lines. To do this we move the cursor there 4764 * briefly, and then move it back. After del_lines() the cursor may 4765 * have moved up (last line deleted), so the current lnum is kept in t. 4766 */ 4767 t = curwin->w_cursor.lnum; 4768 ++curwin->w_cursor.lnum; 4769 del_lines(count - 1, FALSE); 4770 curwin->w_cursor.lnum = t; 4771 4772 /* 4773 * Set the cursor column: 4774 * Vi compatible: use the column of the first join 4775 * vim: use the column of the last join 4776 */ 4777 curwin->w_cursor.col = 4778 (vim_strchr(p_cpo, CPO_JOINCOL) != NULL ? currsize : col); 4779 check_cursor_col(); 4780 4781 #ifdef FEAT_VIRTUALEDIT 4782 curwin->w_cursor.coladd = 0; 4783 #endif 4784 curwin->w_set_curswant = TRUE; 4785 4786 theend: 4787 vim_free(spaces); 4788 #if defined(FEAT_COMMENTS) || defined(PROTO) 4789 if (remove_comments) 4790 vim_free(comments); 4791 #endif 4792 return ret; 4793 } 4794 4795 #ifdef FEAT_COMMENTS 4796 /* 4797 * Return TRUE if the two comment leaders given are the same. "lnum" is 4798 * the first line. White-space is ignored. Note that the whole of 4799 * 'leader1' must match 'leader2_len' characters from 'leader2' -- webb 4800 */ 4801 static int 4802 same_leader( 4803 linenr_T lnum, 4804 int leader1_len, 4805 char_u *leader1_flags, 4806 int leader2_len, 4807 char_u *leader2_flags) 4808 { 4809 int idx1 = 0, idx2 = 0; 4810 char_u *p; 4811 char_u *line1; 4812 char_u *line2; 4813 4814 if (leader1_len == 0) 4815 return (leader2_len == 0); 4816 4817 /* 4818 * If first leader has 'f' flag, the lines can be joined only if the 4819 * second line does not have a leader. 4820 * If first leader has 'e' flag, the lines can never be joined. 4821 * If fist leader has 's' flag, the lines can only be joined if there is 4822 * some text after it and the second line has the 'm' flag. 4823 */ 4824 if (leader1_flags != NULL) 4825 { 4826 for (p = leader1_flags; *p && *p != ':'; ++p) 4827 { 4828 if (*p == COM_FIRST) 4829 return (leader2_len == 0); 4830 if (*p == COM_END) 4831 return FALSE; 4832 if (*p == COM_START) 4833 { 4834 if (*(ml_get(lnum) + leader1_len) == NUL) 4835 return FALSE; 4836 if (leader2_flags == NULL || leader2_len == 0) 4837 return FALSE; 4838 for (p = leader2_flags; *p && *p != ':'; ++p) 4839 if (*p == COM_MIDDLE) 4840 return TRUE; 4841 return FALSE; 4842 } 4843 } 4844 } 4845 4846 /* 4847 * Get current line and next line, compare the leaders. 4848 * The first line has to be saved, only one line can be locked at a time. 4849 */ 4850 line1 = vim_strsave(ml_get(lnum)); 4851 if (line1 != NULL) 4852 { 4853 for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1) 4854 ; 4855 line2 = ml_get(lnum + 1); 4856 for (idx2 = 0; idx2 < leader2_len; ++idx2) 4857 { 4858 if (!VIM_ISWHITE(line2[idx2])) 4859 { 4860 if (line1[idx1++] != line2[idx2]) 4861 break; 4862 } 4863 else 4864 while (VIM_ISWHITE(line1[idx1])) 4865 ++idx1; 4866 } 4867 vim_free(line1); 4868 } 4869 return (idx2 == leader2_len && idx1 == leader1_len); 4870 } 4871 #endif 4872 4873 /* 4874 * Implementation of the format operator 'gq'. 4875 */ 4876 void 4877 op_format( 4878 oparg_T *oap, 4879 int keep_cursor) /* keep cursor on same text char */ 4880 { 4881 long old_line_count = curbuf->b_ml.ml_line_count; 4882 4883 /* Place the cursor where the "gq" or "gw" command was given, so that "u" 4884 * can put it back there. */ 4885 curwin->w_cursor = oap->cursor_start; 4886 4887 if (u_save((linenr_T)(oap->start.lnum - 1), 4888 (linenr_T)(oap->end.lnum + 1)) == FAIL) 4889 return; 4890 curwin->w_cursor = oap->start; 4891 4892 if (oap->is_VIsual) 4893 /* When there is no change: need to remove the Visual selection */ 4894 redraw_curbuf_later(INVERTED); 4895 4896 /* Set '[ mark at the start of the formatted area */ 4897 curbuf->b_op_start = oap->start; 4898 4899 /* For "gw" remember the cursor position and put it back below (adjusted 4900 * for joined and split lines). */ 4901 if (keep_cursor) 4902 saved_cursor = oap->cursor_start; 4903 4904 format_lines(oap->line_count, keep_cursor); 4905 4906 /* 4907 * Leave the cursor at the first non-blank of the last formatted line. 4908 * If the cursor was moved one line back (e.g. with "Q}") go to the next 4909 * line, so "." will do the next lines. 4910 */ 4911 if (oap->end_adjusted && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 4912 ++curwin->w_cursor.lnum; 4913 beginline(BL_WHITE | BL_FIX); 4914 old_line_count = curbuf->b_ml.ml_line_count - old_line_count; 4915 msgmore(old_line_count); 4916 4917 /* put '] mark on the end of the formatted area */ 4918 curbuf->b_op_end = curwin->w_cursor; 4919 4920 if (keep_cursor) 4921 { 4922 curwin->w_cursor = saved_cursor; 4923 saved_cursor.lnum = 0; 4924 } 4925 4926 if (oap->is_VIsual) 4927 { 4928 win_T *wp; 4929 4930 FOR_ALL_WINDOWS(wp) 4931 { 4932 if (wp->w_old_cursor_lnum != 0) 4933 { 4934 /* When lines have been inserted or deleted, adjust the end of 4935 * the Visual area to be redrawn. */ 4936 if (wp->w_old_cursor_lnum > wp->w_old_visual_lnum) 4937 wp->w_old_cursor_lnum += old_line_count; 4938 else 4939 wp->w_old_visual_lnum += old_line_count; 4940 } 4941 } 4942 } 4943 } 4944 4945 #if defined(FEAT_EVAL) || defined(PROTO) 4946 /* 4947 * Implementation of the format operator 'gq' for when using 'formatexpr'. 4948 */ 4949 void 4950 op_formatexpr(oparg_T *oap) 4951 { 4952 if (oap->is_VIsual) 4953 /* When there is no change: need to remove the Visual selection */ 4954 redraw_curbuf_later(INVERTED); 4955 4956 if (fex_format(oap->start.lnum, oap->line_count, NUL) != 0) 4957 /* As documented: when 'formatexpr' returns non-zero fall back to 4958 * internal formatting. */ 4959 op_format(oap, FALSE); 4960 } 4961 4962 int 4963 fex_format( 4964 linenr_T lnum, 4965 long count, 4966 int c) /* character to be inserted */ 4967 { 4968 int use_sandbox = was_set_insecurely((char_u *)"formatexpr", 4969 OPT_LOCAL); 4970 int r; 4971 char_u *fex; 4972 4973 /* 4974 * Set v:lnum to the first line number and v:count to the number of lines. 4975 * Set v:char to the character to be inserted (can be NUL). 4976 */ 4977 set_vim_var_nr(VV_LNUM, lnum); 4978 set_vim_var_nr(VV_COUNT, count); 4979 set_vim_var_char(c); 4980 4981 /* Make a copy, the option could be changed while calling it. */ 4982 fex = vim_strsave(curbuf->b_p_fex); 4983 if (fex == NULL) 4984 return 0; 4985 4986 /* 4987 * Evaluate the function. 4988 */ 4989 if (use_sandbox) 4990 ++sandbox; 4991 r = (int)eval_to_number(fex); 4992 if (use_sandbox) 4993 --sandbox; 4994 4995 set_vim_var_string(VV_CHAR, NULL, -1); 4996 vim_free(fex); 4997 4998 return r; 4999 } 5000 #endif 5001 5002 /* 5003 * Format "line_count" lines, starting at the cursor position. 5004 * When "line_count" is negative, format until the end of the paragraph. 5005 * Lines after the cursor line are saved for undo, caller must have saved the 5006 * first line. 5007 */ 5008 void 5009 format_lines( 5010 linenr_T line_count, 5011 int avoid_fex) /* don't use 'formatexpr' */ 5012 { 5013 int max_len; 5014 int is_not_par; /* current line not part of parag. */ 5015 int next_is_not_par; /* next line not part of paragraph */ 5016 int is_end_par; /* at end of paragraph */ 5017 int prev_is_end_par = FALSE;/* prev. line not part of parag. */ 5018 int next_is_start_par = FALSE; 5019 #ifdef FEAT_COMMENTS 5020 int leader_len = 0; /* leader len of current line */ 5021 int next_leader_len; /* leader len of next line */ 5022 char_u *leader_flags = NULL; /* flags for leader of current line */ 5023 char_u *next_leader_flags; /* flags for leader of next line */ 5024 int do_comments; /* format comments */ 5025 int do_comments_list = 0; /* format comments with 'n' or '2' */ 5026 #endif 5027 int advance = TRUE; 5028 int second_indent = -1; /* indent for second line (comment 5029 * aware) */ 5030 int do_second_indent; 5031 int do_number_indent; 5032 int do_trail_white; 5033 int first_par_line = TRUE; 5034 int smd_save; 5035 long count; 5036 int need_set_indent = TRUE; /* set indent of next paragraph */ 5037 int force_format = FALSE; 5038 int old_State = State; 5039 5040 /* length of a line to force formatting: 3 * 'tw' */ 5041 max_len = comp_textwidth(TRUE) * 3; 5042 5043 /* check for 'q', '2' and '1' in 'formatoptions' */ 5044 #ifdef FEAT_COMMENTS 5045 do_comments = has_format_option(FO_Q_COMS); 5046 #endif 5047 do_second_indent = has_format_option(FO_Q_SECOND); 5048 do_number_indent = has_format_option(FO_Q_NUMBER); 5049 do_trail_white = has_format_option(FO_WHITE_PAR); 5050 5051 /* 5052 * Get info about the previous and current line. 5053 */ 5054 if (curwin->w_cursor.lnum > 1) 5055 is_not_par = fmt_check_par(curwin->w_cursor.lnum - 1 5056 #ifdef FEAT_COMMENTS 5057 , &leader_len, &leader_flags, do_comments 5058 #endif 5059 ); 5060 else 5061 is_not_par = TRUE; 5062 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum 5063 #ifdef FEAT_COMMENTS 5064 , &next_leader_len, &next_leader_flags, do_comments 5065 #endif 5066 ); 5067 is_end_par = (is_not_par || next_is_not_par); 5068 if (!is_end_par && do_trail_white) 5069 is_end_par = !ends_in_white(curwin->w_cursor.lnum - 1); 5070 5071 curwin->w_cursor.lnum--; 5072 for (count = line_count; count != 0 && !got_int; --count) 5073 { 5074 /* 5075 * Advance to next paragraph. 5076 */ 5077 if (advance) 5078 { 5079 curwin->w_cursor.lnum++; 5080 prev_is_end_par = is_end_par; 5081 is_not_par = next_is_not_par; 5082 #ifdef FEAT_COMMENTS 5083 leader_len = next_leader_len; 5084 leader_flags = next_leader_flags; 5085 #endif 5086 } 5087 5088 /* 5089 * The last line to be formatted. 5090 */ 5091 if (count == 1 || curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count) 5092 { 5093 next_is_not_par = TRUE; 5094 #ifdef FEAT_COMMENTS 5095 next_leader_len = 0; 5096 next_leader_flags = NULL; 5097 #endif 5098 } 5099 else 5100 { 5101 next_is_not_par = fmt_check_par(curwin->w_cursor.lnum + 1 5102 #ifdef FEAT_COMMENTS 5103 , &next_leader_len, &next_leader_flags, do_comments 5104 #endif 5105 ); 5106 if (do_number_indent) 5107 next_is_start_par = 5108 (get_number_indent(curwin->w_cursor.lnum + 1) > 0); 5109 } 5110 advance = TRUE; 5111 is_end_par = (is_not_par || next_is_not_par || next_is_start_par); 5112 if (!is_end_par && do_trail_white) 5113 is_end_par = !ends_in_white(curwin->w_cursor.lnum); 5114 5115 /* 5116 * Skip lines that are not in a paragraph. 5117 */ 5118 if (is_not_par) 5119 { 5120 if (line_count < 0) 5121 break; 5122 } 5123 else 5124 { 5125 /* 5126 * For the first line of a paragraph, check indent of second line. 5127 * Don't do this for comments and empty lines. 5128 */ 5129 if (first_par_line 5130 && (do_second_indent || do_number_indent) 5131 && prev_is_end_par 5132 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 5133 { 5134 if (do_second_indent && !LINEEMPTY(curwin->w_cursor.lnum + 1)) 5135 { 5136 #ifdef FEAT_COMMENTS 5137 if (leader_len == 0 && next_leader_len == 0) 5138 { 5139 /* no comment found */ 5140 #endif 5141 second_indent = 5142 get_indent_lnum(curwin->w_cursor.lnum + 1); 5143 #ifdef FEAT_COMMENTS 5144 } 5145 else 5146 { 5147 second_indent = next_leader_len; 5148 do_comments_list = 1; 5149 } 5150 #endif 5151 } 5152 else if (do_number_indent) 5153 { 5154 #ifdef FEAT_COMMENTS 5155 if (leader_len == 0 && next_leader_len == 0) 5156 { 5157 /* no comment found */ 5158 #endif 5159 second_indent = 5160 get_number_indent(curwin->w_cursor.lnum); 5161 #ifdef FEAT_COMMENTS 5162 } 5163 else 5164 { 5165 /* get_number_indent() is now "comment aware"... */ 5166 second_indent = 5167 get_number_indent(curwin->w_cursor.lnum); 5168 do_comments_list = 1; 5169 } 5170 #endif 5171 } 5172 } 5173 5174 /* 5175 * When the comment leader changes, it's the end of the paragraph. 5176 */ 5177 if (curwin->w_cursor.lnum >= curbuf->b_ml.ml_line_count 5178 #ifdef FEAT_COMMENTS 5179 || !same_leader(curwin->w_cursor.lnum, 5180 leader_len, leader_flags, 5181 next_leader_len, next_leader_flags) 5182 #endif 5183 ) 5184 is_end_par = TRUE; 5185 5186 /* 5187 * If we have got to the end of a paragraph, or the line is 5188 * getting long, format it. 5189 */ 5190 if (is_end_par || force_format) 5191 { 5192 if (need_set_indent) 5193 /* replace indent in first line with minimal number of 5194 * tabs and spaces, according to current options */ 5195 (void)set_indent(get_indent(), SIN_CHANGED); 5196 5197 /* put cursor on last non-space */ 5198 State = NORMAL; /* don't go past end-of-line */ 5199 coladvance((colnr_T)MAXCOL); 5200 while (curwin->w_cursor.col && vim_isspace(gchar_cursor())) 5201 dec_cursor(); 5202 5203 /* do the formatting, without 'showmode' */ 5204 State = INSERT; /* for open_line() */ 5205 smd_save = p_smd; 5206 p_smd = FALSE; 5207 insertchar(NUL, INSCHAR_FORMAT 5208 #ifdef FEAT_COMMENTS 5209 + (do_comments ? INSCHAR_DO_COM : 0) 5210 + (do_comments && do_comments_list 5211 ? INSCHAR_COM_LIST : 0) 5212 #endif 5213 + (avoid_fex ? INSCHAR_NO_FEX : 0), second_indent); 5214 State = old_State; 5215 p_smd = smd_save; 5216 second_indent = -1; 5217 /* at end of par.: need to set indent of next par. */ 5218 need_set_indent = is_end_par; 5219 if (is_end_par) 5220 { 5221 /* When called with a negative line count, break at the 5222 * end of the paragraph. */ 5223 if (line_count < 0) 5224 break; 5225 first_par_line = TRUE; 5226 } 5227 force_format = FALSE; 5228 } 5229 5230 /* 5231 * When still in same paragraph, join the lines together. But 5232 * first delete the leader from the second line. 5233 */ 5234 if (!is_end_par) 5235 { 5236 advance = FALSE; 5237 curwin->w_cursor.lnum++; 5238 curwin->w_cursor.col = 0; 5239 if (line_count < 0 && u_save_cursor() == FAIL) 5240 break; 5241 #ifdef FEAT_COMMENTS 5242 if (next_leader_len > 0) 5243 { 5244 (void)del_bytes((long)next_leader_len, FALSE, FALSE); 5245 mark_col_adjust(curwin->w_cursor.lnum, (colnr_T)0, 0L, 5246 (long)-next_leader_len); 5247 } else 5248 #endif 5249 if (second_indent > 0) /* the "leader" for FO_Q_SECOND */ 5250 { 5251 int indent = getwhitecols_curline(); 5252 5253 if (indent > 0) 5254 { 5255 (void)del_bytes(indent, FALSE, FALSE); 5256 mark_col_adjust(curwin->w_cursor.lnum, 5257 (colnr_T)0, 0L, (long)-indent); 5258 } 5259 } 5260 curwin->w_cursor.lnum--; 5261 if (do_join(2, TRUE, FALSE, FALSE, FALSE) == FAIL) 5262 { 5263 beep_flush(); 5264 break; 5265 } 5266 first_par_line = FALSE; 5267 /* If the line is getting long, format it next time */ 5268 if (STRLEN(ml_get_curline()) > (size_t)max_len) 5269 force_format = TRUE; 5270 else 5271 force_format = FALSE; 5272 } 5273 } 5274 line_breakcheck(); 5275 } 5276 } 5277 5278 /* 5279 * Return TRUE if line "lnum" ends in a white character. 5280 */ 5281 static int 5282 ends_in_white(linenr_T lnum) 5283 { 5284 char_u *s = ml_get(lnum); 5285 size_t l; 5286 5287 if (*s == NUL) 5288 return FALSE; 5289 /* Don't use STRLEN() inside VIM_ISWHITE(), SAS/C complains: "macro 5290 * invocation may call function multiple times". */ 5291 l = STRLEN(s) - 1; 5292 return VIM_ISWHITE(s[l]); 5293 } 5294 5295 /* 5296 * Blank lines, and lines containing only the comment leader, are left 5297 * untouched by the formatting. The function returns TRUE in this 5298 * case. It also returns TRUE when a line starts with the end of a comment 5299 * ('e' in comment flags), so that this line is skipped, and not joined to the 5300 * previous line. A new paragraph starts after a blank line, or when the 5301 * comment leader changes -- webb. 5302 */ 5303 #ifdef FEAT_COMMENTS 5304 static int 5305 fmt_check_par( 5306 linenr_T lnum, 5307 int *leader_len, 5308 char_u **leader_flags, 5309 int do_comments) 5310 { 5311 char_u *flags = NULL; /* init for GCC */ 5312 char_u *ptr; 5313 5314 ptr = ml_get(lnum); 5315 if (do_comments) 5316 *leader_len = get_leader_len(ptr, leader_flags, FALSE, TRUE); 5317 else 5318 *leader_len = 0; 5319 5320 if (*leader_len > 0) 5321 { 5322 /* 5323 * Search for 'e' flag in comment leader flags. 5324 */ 5325 flags = *leader_flags; 5326 while (*flags && *flags != ':' && *flags != COM_END) 5327 ++flags; 5328 } 5329 5330 return (*skipwhite(ptr + *leader_len) == NUL 5331 || (*leader_len > 0 && *flags == COM_END) 5332 || startPS(lnum, NUL, FALSE)); 5333 } 5334 #else 5335 static int 5336 fmt_check_par(linenr_T lnum) 5337 { 5338 return (*skipwhite(ml_get(lnum)) == NUL || startPS(lnum, NUL, FALSE)); 5339 } 5340 #endif 5341 5342 /* 5343 * Return TRUE when a paragraph starts in line "lnum". Return FALSE when the 5344 * previous line is in the same paragraph. Used for auto-formatting. 5345 */ 5346 int 5347 paragraph_start(linenr_T lnum) 5348 { 5349 char_u *p; 5350 #ifdef FEAT_COMMENTS 5351 int leader_len = 0; /* leader len of current line */ 5352 char_u *leader_flags = NULL; /* flags for leader of current line */ 5353 int next_leader_len; /* leader len of next line */ 5354 char_u *next_leader_flags; /* flags for leader of next line */ 5355 int do_comments; /* format comments */ 5356 #endif 5357 5358 if (lnum <= 1) 5359 return TRUE; /* start of the file */ 5360 5361 p = ml_get(lnum - 1); 5362 if (*p == NUL) 5363 return TRUE; /* after empty line */ 5364 5365 #ifdef FEAT_COMMENTS 5366 do_comments = has_format_option(FO_Q_COMS); 5367 #endif 5368 if (fmt_check_par(lnum - 1 5369 #ifdef FEAT_COMMENTS 5370 , &leader_len, &leader_flags, do_comments 5371 #endif 5372 )) 5373 return TRUE; /* after non-paragraph line */ 5374 5375 if (fmt_check_par(lnum 5376 #ifdef FEAT_COMMENTS 5377 , &next_leader_len, &next_leader_flags, do_comments 5378 #endif 5379 )) 5380 return TRUE; /* "lnum" is not a paragraph line */ 5381 5382 if (has_format_option(FO_WHITE_PAR) && !ends_in_white(lnum - 1)) 5383 return TRUE; /* missing trailing space in previous line. */ 5384 5385 if (has_format_option(FO_Q_NUMBER) && (get_number_indent(lnum) > 0)) 5386 return TRUE; /* numbered item starts in "lnum". */ 5387 5388 #ifdef FEAT_COMMENTS 5389 if (!same_leader(lnum - 1, leader_len, leader_flags, 5390 next_leader_len, next_leader_flags)) 5391 return TRUE; /* change of comment leader. */ 5392 #endif 5393 5394 return FALSE; 5395 } 5396 5397 /* 5398 * prepare a few things for block mode yank/delete/tilde 5399 * 5400 * for delete: 5401 * - textlen includes the first/last char to be (partly) deleted 5402 * - start/endspaces is the number of columns that are taken by the 5403 * first/last deleted char minus the number of columns that have to be 5404 * deleted. 5405 * for yank and tilde: 5406 * - textlen includes the first/last char to be wholly yanked 5407 * - start/endspaces is the number of columns of the first/last yanked char 5408 * that are to be yanked. 5409 */ 5410 static void 5411 block_prep( 5412 oparg_T *oap, 5413 struct block_def *bdp, 5414 linenr_T lnum, 5415 int is_del) 5416 { 5417 int incr = 0; 5418 char_u *pend; 5419 char_u *pstart; 5420 char_u *line; 5421 char_u *prev_pstart; 5422 char_u *prev_pend; 5423 5424 bdp->startspaces = 0; 5425 bdp->endspaces = 0; 5426 bdp->textlen = 0; 5427 bdp->start_vcol = 0; 5428 bdp->end_vcol = 0; 5429 #ifdef FEAT_VISUALEXTRA 5430 bdp->is_short = FALSE; 5431 bdp->is_oneChar = FALSE; 5432 bdp->pre_whitesp = 0; 5433 bdp->pre_whitesp_c = 0; 5434 bdp->end_char_vcols = 0; 5435 #endif 5436 bdp->start_char_vcols = 0; 5437 5438 line = ml_get(lnum); 5439 pstart = line; 5440 prev_pstart = line; 5441 while (bdp->start_vcol < oap->start_vcol && *pstart) 5442 { 5443 /* Count a tab for what it's worth (if list mode not on) */ 5444 incr = lbr_chartabsize(line, pstart, (colnr_T)bdp->start_vcol); 5445 bdp->start_vcol += incr; 5446 #ifdef FEAT_VISUALEXTRA 5447 if (VIM_ISWHITE(*pstart)) 5448 { 5449 bdp->pre_whitesp += incr; 5450 bdp->pre_whitesp_c++; 5451 } 5452 else 5453 { 5454 bdp->pre_whitesp = 0; 5455 bdp->pre_whitesp_c = 0; 5456 } 5457 #endif 5458 prev_pstart = pstart; 5459 MB_PTR_ADV(pstart); 5460 } 5461 bdp->start_char_vcols = incr; 5462 if (bdp->start_vcol < oap->start_vcol) /* line too short */ 5463 { 5464 bdp->end_vcol = bdp->start_vcol; 5465 #ifdef FEAT_VISUALEXTRA 5466 bdp->is_short = TRUE; 5467 #endif 5468 if (!is_del || oap->op_type == OP_APPEND) 5469 bdp->endspaces = oap->end_vcol - oap->start_vcol + 1; 5470 } 5471 else 5472 { 5473 /* notice: this converts partly selected Multibyte characters to 5474 * spaces, too. */ 5475 bdp->startspaces = bdp->start_vcol - oap->start_vcol; 5476 if (is_del && bdp->startspaces) 5477 bdp->startspaces = bdp->start_char_vcols - bdp->startspaces; 5478 pend = pstart; 5479 bdp->end_vcol = bdp->start_vcol; 5480 if (bdp->end_vcol > oap->end_vcol) /* it's all in one character */ 5481 { 5482 #ifdef FEAT_VISUALEXTRA 5483 bdp->is_oneChar = TRUE; 5484 #endif 5485 if (oap->op_type == OP_INSERT) 5486 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; 5487 else if (oap->op_type == OP_APPEND) 5488 { 5489 bdp->startspaces += oap->end_vcol - oap->start_vcol + 1; 5490 bdp->endspaces = bdp->start_char_vcols - bdp->startspaces; 5491 } 5492 else 5493 { 5494 bdp->startspaces = oap->end_vcol - oap->start_vcol + 1; 5495 if (is_del && oap->op_type != OP_LSHIFT) 5496 { 5497 /* just putting the sum of those two into 5498 * bdp->startspaces doesn't work for Visual replace, 5499 * so we have to split the tab in two */ 5500 bdp->startspaces = bdp->start_char_vcols 5501 - (bdp->start_vcol - oap->start_vcol); 5502 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; 5503 } 5504 } 5505 } 5506 else 5507 { 5508 prev_pend = pend; 5509 while (bdp->end_vcol <= oap->end_vcol && *pend != NUL) 5510 { 5511 /* Count a tab for what it's worth (if list mode not on) */ 5512 prev_pend = pend; 5513 incr = lbr_chartabsize_adv(line, &pend, (colnr_T)bdp->end_vcol); 5514 bdp->end_vcol += incr; 5515 } 5516 if (bdp->end_vcol <= oap->end_vcol 5517 && (!is_del 5518 || oap->op_type == OP_APPEND 5519 || oap->op_type == OP_REPLACE)) /* line too short */ 5520 { 5521 #ifdef FEAT_VISUALEXTRA 5522 bdp->is_short = TRUE; 5523 #endif 5524 /* Alternative: include spaces to fill up the block. 5525 * Disadvantage: can lead to trailing spaces when the line is 5526 * short where the text is put */ 5527 /* if (!is_del || oap->op_type == OP_APPEND) */ 5528 if (oap->op_type == OP_APPEND || virtual_op) 5529 bdp->endspaces = oap->end_vcol - bdp->end_vcol 5530 + oap->inclusive; 5531 else 5532 bdp->endspaces = 0; /* replace doesn't add characters */ 5533 } 5534 else if (bdp->end_vcol > oap->end_vcol) 5535 { 5536 bdp->endspaces = bdp->end_vcol - oap->end_vcol - 1; 5537 if (!is_del && bdp->endspaces) 5538 { 5539 bdp->endspaces = incr - bdp->endspaces; 5540 if (pend != pstart) 5541 pend = prev_pend; 5542 } 5543 } 5544 } 5545 #ifdef FEAT_VISUALEXTRA 5546 bdp->end_char_vcols = incr; 5547 #endif 5548 if (is_del && bdp->startspaces) 5549 pstart = prev_pstart; 5550 bdp->textlen = (int)(pend - pstart); 5551 } 5552 bdp->textcol = (colnr_T) (pstart - line); 5553 bdp->textstart = pstart; 5554 } 5555 5556 /* 5557 * Handle the add/subtract operator. 5558 */ 5559 void 5560 op_addsub( 5561 oparg_T *oap, 5562 linenr_T Prenum1, /* Amount of add/subtract */ 5563 int g_cmd) /* was g<c-a>/g<c-x> */ 5564 { 5565 pos_T pos; 5566 struct block_def bd; 5567 int change_cnt = 0; 5568 linenr_T amount = Prenum1; 5569 5570 if (!VIsual_active) 5571 { 5572 pos = curwin->w_cursor; 5573 if (u_save_cursor() == FAIL) 5574 return; 5575 change_cnt = do_addsub(oap->op_type, &pos, 0, amount); 5576 if (change_cnt) 5577 changed_lines(pos.lnum, 0, pos.lnum + 1, 0L); 5578 } 5579 else 5580 { 5581 int one_change; 5582 int length; 5583 pos_T startpos; 5584 5585 if (u_save((linenr_T)(oap->start.lnum - 1), 5586 (linenr_T)(oap->end.lnum + 1)) == FAIL) 5587 return; 5588 5589 pos = oap->start; 5590 for (; pos.lnum <= oap->end.lnum; ++pos.lnum) 5591 { 5592 if (oap->block_mode) /* Visual block mode */ 5593 { 5594 block_prep(oap, &bd, pos.lnum, FALSE); 5595 pos.col = bd.textcol; 5596 length = bd.textlen; 5597 } 5598 else if (oap->motion_type == MLINE) 5599 { 5600 curwin->w_cursor.col = 0; 5601 pos.col = 0; 5602 length = (colnr_T)STRLEN(ml_get(pos.lnum)); 5603 } 5604 else /* oap->motion_type == MCHAR */ 5605 { 5606 if (pos.lnum == oap->start.lnum && !oap->inclusive) 5607 dec(&(oap->end)); 5608 length = (colnr_T)STRLEN(ml_get(pos.lnum)); 5609 pos.col = 0; 5610 if (pos.lnum == oap->start.lnum) 5611 { 5612 pos.col += oap->start.col; 5613 length -= oap->start.col; 5614 } 5615 if (pos.lnum == oap->end.lnum) 5616 { 5617 length = (int)STRLEN(ml_get(oap->end.lnum)); 5618 if (oap->end.col >= length) 5619 oap->end.col = length - 1; 5620 length = oap->end.col - pos.col + 1; 5621 } 5622 } 5623 one_change = do_addsub(oap->op_type, &pos, length, amount); 5624 if (one_change) 5625 { 5626 /* Remember the start position of the first change. */ 5627 if (change_cnt == 0) 5628 startpos = curbuf->b_op_start; 5629 ++change_cnt; 5630 } 5631 5632 #ifdef FEAT_NETBEANS_INTG 5633 if (netbeans_active() && one_change) 5634 { 5635 char_u *ptr = ml_get_buf(curbuf, pos.lnum, FALSE); 5636 5637 netbeans_removed(curbuf, pos.lnum, pos.col, (long)length); 5638 netbeans_inserted(curbuf, pos.lnum, pos.col, 5639 &ptr[pos.col], length); 5640 } 5641 #endif 5642 if (g_cmd && one_change) 5643 amount += Prenum1; 5644 } 5645 if (change_cnt) 5646 changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L); 5647 5648 if (!change_cnt && oap->is_VIsual) 5649 /* No change: need to remove the Visual selection */ 5650 redraw_curbuf_later(INVERTED); 5651 5652 /* Set '[ mark if something changed. Keep the last end 5653 * position from do_addsub(). */ 5654 if (change_cnt > 0) 5655 curbuf->b_op_start = startpos; 5656 5657 if (change_cnt > p_report) 5658 { 5659 if (change_cnt == 1) 5660 MSG(_("1 line changed")); 5661 else 5662 smsg((char_u *)_("%ld lines changed"), change_cnt); 5663 } 5664 } 5665 } 5666 5667 /* 5668 * Add or subtract 'Prenum1' from a number in a line 5669 * op_type is OP_NR_ADD or OP_NR_SUB 5670 * 5671 * Returns TRUE if some character was changed. 5672 */ 5673 static int 5674 do_addsub( 5675 int op_type, 5676 pos_T *pos, 5677 int length, 5678 linenr_T Prenum1) 5679 { 5680 int col; 5681 char_u *buf1; 5682 char_u buf2[NUMBUFLEN]; 5683 int pre; /* 'X'/'x': hex; '0': octal; 'B'/'b': bin */ 5684 static int hexupper = FALSE; /* 0xABC */ 5685 uvarnumber_T n; 5686 uvarnumber_T oldn; 5687 char_u *ptr; 5688 int c; 5689 int todel; 5690 int dohex; 5691 int dooct; 5692 int dobin; 5693 int doalp; 5694 int firstdigit; 5695 int subtract; 5696 int negative = FALSE; 5697 int was_positive = TRUE; 5698 int visual = VIsual_active; 5699 int did_change = FALSE; 5700 pos_T save_cursor = curwin->w_cursor; 5701 int maxlen = 0; 5702 pos_T startpos; 5703 pos_T endpos; 5704 5705 dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */ 5706 dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */ 5707 dobin = (vim_strchr(curbuf->b_p_nf, 'b') != NULL); /* "Bin" */ 5708 doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */ 5709 5710 curwin->w_cursor = *pos; 5711 ptr = ml_get(pos->lnum); 5712 col = pos->col; 5713 5714 if (*ptr == NUL) 5715 goto theend; 5716 5717 /* 5718 * First check if we are on a hexadecimal number, after the "0x". 5719 */ 5720 if (!VIsual_active) 5721 { 5722 if (dobin) 5723 while (col > 0 && vim_isbdigit(ptr[col])) 5724 { 5725 --col; 5726 #ifdef FEAT_MBYTE 5727 if (has_mbyte) 5728 col -= (*mb_head_off)(ptr, ptr + col); 5729 #endif 5730 } 5731 5732 if (dohex) 5733 while (col > 0 && vim_isxdigit(ptr[col])) 5734 { 5735 --col; 5736 #ifdef FEAT_MBYTE 5737 if (has_mbyte) 5738 col -= (*mb_head_off)(ptr, ptr + col); 5739 #endif 5740 } 5741 5742 if ( dobin 5743 && dohex 5744 && ! ((col > 0 5745 && (ptr[col] == 'X' 5746 || ptr[col] == 'x') 5747 && ptr[col - 1] == '0' 5748 #ifdef FEAT_MBYTE 5749 && (!has_mbyte || 5750 !(*mb_head_off)(ptr, ptr + col - 1)) 5751 #endif 5752 && vim_isxdigit(ptr[col + 1])))) 5753 { 5754 5755 /* In case of binary/hexadecimal pattern overlap match, rescan */ 5756 5757 col = pos->col; 5758 5759 while (col > 0 && vim_isdigit(ptr[col])) 5760 { 5761 col--; 5762 #ifdef FEAT_MBYTE 5763 if (has_mbyte) 5764 col -= (*mb_head_off)(ptr, ptr + col); 5765 #endif 5766 } 5767 } 5768 5769 if (( dohex 5770 && col > 0 5771 && (ptr[col] == 'X' 5772 || ptr[col] == 'x') 5773 && ptr[col - 1] == '0' 5774 #ifdef FEAT_MBYTE 5775 && (!has_mbyte || 5776 !(*mb_head_off)(ptr, ptr + col - 1)) 5777 #endif 5778 && vim_isxdigit(ptr[col + 1])) || 5779 ( dobin 5780 && col > 0 5781 && (ptr[col] == 'B' 5782 || ptr[col] == 'b') 5783 && ptr[col - 1] == '0' 5784 #ifdef FEAT_MBYTE 5785 && (!has_mbyte || 5786 !(*mb_head_off)(ptr, ptr + col - 1)) 5787 #endif 5788 && vim_isbdigit(ptr[col + 1]))) 5789 { 5790 /* Found hexadecimal or binary number, move to its start. */ 5791 --col; 5792 #ifdef FEAT_MBYTE 5793 if (has_mbyte) 5794 col -= (*mb_head_off)(ptr, ptr + col); 5795 #endif 5796 } 5797 else 5798 { 5799 /* 5800 * Search forward and then backward to find the start of number. 5801 */ 5802 col = pos->col; 5803 5804 while (ptr[col] != NUL 5805 && !vim_isdigit(ptr[col]) 5806 && !(doalp && ASCII_ISALPHA(ptr[col]))) 5807 col += MB_PTR2LEN(ptr + col); 5808 5809 while (col > 0 5810 && vim_isdigit(ptr[col - 1]) 5811 && !(doalp && ASCII_ISALPHA(ptr[col]))) 5812 { 5813 --col; 5814 #ifdef FEAT_MBYTE 5815 if (has_mbyte) 5816 col -= (*mb_head_off)(ptr, ptr + col); 5817 #endif 5818 } 5819 } 5820 } 5821 5822 if (visual) 5823 { 5824 while (ptr[col] != NUL && length > 0 5825 && !vim_isdigit(ptr[col]) 5826 && !(doalp && ASCII_ISALPHA(ptr[col]))) 5827 { 5828 int mb_len = MB_PTR2LEN(ptr + col); 5829 5830 col += mb_len; 5831 length -= mb_len; 5832 } 5833 5834 if (length == 0) 5835 goto theend; 5836 5837 if (col > pos->col && ptr[col - 1] == '-' 5838 #ifdef FEAT_MBYTE 5839 && (!has_mbyte || 5840 !(*mb_head_off)(ptr, ptr + col - 1)) 5841 #endif 5842 ) 5843 { 5844 negative = TRUE; 5845 was_positive = FALSE; 5846 } 5847 } 5848 5849 /* 5850 * If a number was found, and saving for undo works, replace the number. 5851 */ 5852 firstdigit = ptr[col]; 5853 if (!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit))) 5854 { 5855 beep_flush(); 5856 goto theend; 5857 } 5858 5859 if (doalp && ASCII_ISALPHA(firstdigit)) 5860 { 5861 /* decrement or increment alphabetic character */ 5862 if (op_type == OP_NR_SUB) 5863 { 5864 if (CharOrd(firstdigit) < Prenum1) 5865 { 5866 if (isupper(firstdigit)) 5867 firstdigit = 'A'; 5868 else 5869 firstdigit = 'a'; 5870 } 5871 else 5872 #ifdef EBCDIC 5873 firstdigit = EBCDIC_CHAR_ADD(firstdigit, -Prenum1); 5874 #else 5875 firstdigit -= Prenum1; 5876 #endif 5877 } 5878 else 5879 { 5880 if (26 - CharOrd(firstdigit) - 1 < Prenum1) 5881 { 5882 if (isupper(firstdigit)) 5883 firstdigit = 'Z'; 5884 else 5885 firstdigit = 'z'; 5886 } 5887 else 5888 #ifdef EBCDIC 5889 firstdigit = EBCDIC_CHAR_ADD(firstdigit, Prenum1); 5890 #else 5891 firstdigit += Prenum1; 5892 #endif 5893 } 5894 curwin->w_cursor.col = col; 5895 if (!did_change) 5896 startpos = curwin->w_cursor; 5897 did_change = TRUE; 5898 (void)del_char(FALSE); 5899 ins_char(firstdigit); 5900 endpos = curwin->w_cursor; 5901 curwin->w_cursor.col = col; 5902 } 5903 else 5904 { 5905 if (col > 0 && ptr[col - 1] == '-' 5906 #ifdef FEAT_MBYTE 5907 && (!has_mbyte || 5908 !(*mb_head_off)(ptr, ptr + col - 1)) 5909 #endif 5910 && !visual) 5911 { 5912 /* negative number */ 5913 --col; 5914 negative = TRUE; 5915 } 5916 /* get the number value (unsigned) */ 5917 if (visual && VIsual_mode != 'V') 5918 maxlen = (curbuf->b_visual.vi_curswant == MAXCOL 5919 ? (int)STRLEN(ptr) - col 5920 : length); 5921 5922 vim_str2nr(ptr + col, &pre, &length, 5923 0 + (dobin ? STR2NR_BIN : 0) 5924 + (dooct ? STR2NR_OCT : 0) 5925 + (dohex ? STR2NR_HEX : 0), 5926 NULL, &n, maxlen); 5927 5928 /* ignore leading '-' for hex and octal and bin numbers */ 5929 if (pre && negative) 5930 { 5931 ++col; 5932 --length; 5933 negative = FALSE; 5934 } 5935 /* add or subtract */ 5936 subtract = FALSE; 5937 if (op_type == OP_NR_SUB) 5938 subtract ^= TRUE; 5939 if (negative) 5940 subtract ^= TRUE; 5941 5942 oldn = n; 5943 if (subtract) 5944 n -= (uvarnumber_T)Prenum1; 5945 else 5946 n += (uvarnumber_T)Prenum1; 5947 /* handle wraparound for decimal numbers */ 5948 if (!pre) 5949 { 5950 if (subtract) 5951 { 5952 if (n > oldn) 5953 { 5954 n = 1 + (n ^ (uvarnumber_T)-1); 5955 negative ^= TRUE; 5956 } 5957 } 5958 else 5959 { 5960 /* add */ 5961 if (n < oldn) 5962 { 5963 n = (n ^ (uvarnumber_T)-1); 5964 negative ^= TRUE; 5965 } 5966 } 5967 if (n == 0) 5968 negative = FALSE; 5969 } 5970 5971 if (visual && !was_positive && !negative && col > 0) 5972 { 5973 /* need to remove the '-' */ 5974 col--; 5975 length++; 5976 } 5977 5978 /* 5979 * Delete the old number. 5980 */ 5981 curwin->w_cursor.col = col; 5982 if (!did_change) 5983 startpos = curwin->w_cursor; 5984 did_change = TRUE; 5985 todel = length; 5986 c = gchar_cursor(); 5987 /* 5988 * Don't include the '-' in the length, only the length of the 5989 * part after it is kept the same. 5990 */ 5991 if (c == '-') 5992 --length; 5993 while (todel-- > 0) 5994 { 5995 if (c < 0x100 && isalpha(c)) 5996 { 5997 if (isupper(c)) 5998 hexupper = TRUE; 5999 else 6000 hexupper = FALSE; 6001 } 6002 /* del_char() will mark line needing displaying */ 6003 (void)del_char(FALSE); 6004 c = gchar_cursor(); 6005 } 6006 6007 /* 6008 * Prepare the leading characters in buf1[]. 6009 * When there are many leading zeros it could be very long. 6010 * Allocate a bit too much. 6011 */ 6012 buf1 = alloc((unsigned)length + NUMBUFLEN); 6013 if (buf1 == NULL) 6014 goto theend; 6015 ptr = buf1; 6016 if (negative && (!visual || was_positive)) 6017 { 6018 *ptr++ = '-'; 6019 } 6020 if (pre) 6021 { 6022 *ptr++ = '0'; 6023 --length; 6024 } 6025 if (pre == 'b' || pre == 'B' || 6026 pre == 'x' || pre == 'X') 6027 { 6028 *ptr++ = pre; 6029 --length; 6030 } 6031 6032 /* 6033 * Put the number characters in buf2[]. 6034 */ 6035 if (pre == 'b' || pre == 'B') 6036 { 6037 int i; 6038 int bit = 0; 6039 int bits = sizeof(uvarnumber_T) * 8; 6040 6041 /* leading zeros */ 6042 for (bit = bits; bit > 0; bit--) 6043 if ((n >> (bit - 1)) & 0x1) break; 6044 6045 for (i = 0; bit > 0; bit--) 6046 buf2[i++] = ((n >> (bit - 1)) & 0x1) ? '1' : '0'; 6047 6048 buf2[i] = '\0'; 6049 } 6050 else if (pre == 0) 6051 vim_snprintf((char *)buf2, NUMBUFLEN, "%llu", 6052 (long long unsigned)n); 6053 else if (pre == '0') 6054 vim_snprintf((char *)buf2, NUMBUFLEN, "%llo", 6055 (long long unsigned)n); 6056 else if (pre && hexupper) 6057 vim_snprintf((char *)buf2, NUMBUFLEN, "%llX", 6058 (long long unsigned)n); 6059 else 6060 vim_snprintf((char *)buf2, NUMBUFLEN, "%llx", 6061 (long long unsigned)n); 6062 length -= (int)STRLEN(buf2); 6063 6064 /* 6065 * Adjust number of zeros to the new number of digits, so the 6066 * total length of the number remains the same. 6067 * Don't do this when 6068 * the result may look like an octal number. 6069 */ 6070 if (firstdigit == '0' && !(dooct && pre == 0)) 6071 while (length-- > 0) 6072 *ptr++ = '0'; 6073 *ptr = NUL; 6074 STRCAT(buf1, buf2); 6075 ins_str(buf1); /* insert the new number */ 6076 vim_free(buf1); 6077 endpos = curwin->w_cursor; 6078 if (did_change && curwin->w_cursor.col) 6079 --curwin->w_cursor.col; 6080 } 6081 6082 if (did_change) 6083 { 6084 /* set the '[ and '] marks */ 6085 curbuf->b_op_start = startpos; 6086 curbuf->b_op_end = endpos; 6087 if (curbuf->b_op_end.col > 0) 6088 --curbuf->b_op_end.col; 6089 } 6090 6091 theend: 6092 if (visual) 6093 curwin->w_cursor = save_cursor; 6094 else if (did_change) 6095 curwin->w_set_curswant = TRUE; 6096 6097 return did_change; 6098 } 6099 6100 #ifdef FEAT_VIMINFO 6101 6102 static yankreg_T *y_read_regs = NULL; 6103 6104 #define REG_PREVIOUS 1 6105 #define REG_EXEC 2 6106 6107 /* 6108 * Prepare for reading viminfo registers when writing viminfo later. 6109 */ 6110 void 6111 prepare_viminfo_registers(void) 6112 { 6113 y_read_regs = (yankreg_T *)alloc_clear(NUM_REGISTERS 6114 * (int)sizeof(yankreg_T)); 6115 } 6116 6117 void 6118 finish_viminfo_registers(void) 6119 { 6120 int i; 6121 int j; 6122 6123 if (y_read_regs != NULL) 6124 { 6125 for (i = 0; i < NUM_REGISTERS; ++i) 6126 if (y_read_regs[i].y_array != NULL) 6127 { 6128 for (j = 0; j < y_read_regs[i].y_size; j++) 6129 vim_free(y_read_regs[i].y_array[j]); 6130 vim_free(y_read_regs[i].y_array); 6131 } 6132 VIM_CLEAR(y_read_regs); 6133 } 6134 } 6135 6136 int 6137 read_viminfo_register(vir_T *virp, int force) 6138 { 6139 int eof; 6140 int do_it = TRUE; 6141 int size; 6142 int limit; 6143 int i; 6144 int set_prev = FALSE; 6145 char_u *str; 6146 char_u **array = NULL; 6147 int new_type = MCHAR; /* init to shut up compiler */ 6148 colnr_T new_width = 0; /* init to shut up compiler */ 6149 6150 /* We only get here (hopefully) if line[0] == '"' */ 6151 str = virp->vir_line + 1; 6152 6153 /* If the line starts with "" this is the y_previous register. */ 6154 if (*str == '"') 6155 { 6156 set_prev = TRUE; 6157 str++; 6158 } 6159 6160 if (!ASCII_ISALNUM(*str) && *str != '-') 6161 { 6162 if (viminfo_error("E577: ", _("Illegal register name"), virp->vir_line)) 6163 return TRUE; /* too many errors, pretend end-of-file */ 6164 do_it = FALSE; 6165 } 6166 get_yank_register(*str++, FALSE); 6167 if (!force && y_current->y_array != NULL) 6168 do_it = FALSE; 6169 6170 if (*str == '@') 6171 { 6172 /* "x@: register x used for @@ */ 6173 if (force || execreg_lastc == NUL) 6174 execreg_lastc = str[-1]; 6175 } 6176 6177 size = 0; 6178 limit = 100; /* Optimized for registers containing <= 100 lines */ 6179 if (do_it) 6180 { 6181 /* 6182 * Build the new register in array[]. 6183 * y_array is kept as-is until done. 6184 * The "do_it" flag is reset when something is wrong, in which case 6185 * array[] needs to be freed. 6186 */ 6187 if (set_prev) 6188 y_previous = y_current; 6189 array = (char_u **)alloc((unsigned)(limit * sizeof(char_u *))); 6190 str = skipwhite(skiptowhite(str)); 6191 if (STRNCMP(str, "CHAR", 4) == 0) 6192 new_type = MCHAR; 6193 else if (STRNCMP(str, "BLOCK", 5) == 0) 6194 new_type = MBLOCK; 6195 else 6196 new_type = MLINE; 6197 /* get the block width; if it's missing we get a zero, which is OK */ 6198 str = skipwhite(skiptowhite(str)); 6199 new_width = getdigits(&str); 6200 } 6201 6202 while (!(eof = viminfo_readline(virp)) 6203 && (virp->vir_line[0] == TAB || virp->vir_line[0] == '<')) 6204 { 6205 if (do_it) 6206 { 6207 if (size == limit) 6208 { 6209 char_u **new_array = (char_u **) 6210 alloc((unsigned)(limit * 2 * sizeof(char_u *))); 6211 6212 if (new_array == NULL) 6213 { 6214 do_it = FALSE; 6215 break; 6216 } 6217 for (i = 0; i < limit; i++) 6218 new_array[i] = array[i]; 6219 vim_free(array); 6220 array = new_array; 6221 limit *= 2; 6222 } 6223 str = viminfo_readstring(virp, 1, TRUE); 6224 if (str != NULL) 6225 array[size++] = str; 6226 else 6227 /* error, don't store the result */ 6228 do_it = FALSE; 6229 } 6230 } 6231 6232 if (do_it) 6233 { 6234 /* free y_array[] */ 6235 for (i = 0; i < y_current->y_size; i++) 6236 vim_free(y_current->y_array[i]); 6237 vim_free(y_current->y_array); 6238 6239 y_current->y_type = new_type; 6240 y_current->y_width = new_width; 6241 y_current->y_size = size; 6242 y_current->y_time_set = 0; 6243 if (size == 0) 6244 { 6245 y_current->y_array = NULL; 6246 } 6247 else 6248 { 6249 /* Move the lines from array[] to y_array[]. */ 6250 y_current->y_array = 6251 (char_u **)alloc((unsigned)(size * sizeof(char_u *))); 6252 for (i = 0; i < size; i++) 6253 { 6254 if (y_current->y_array == NULL) 6255 vim_free(array[i]); 6256 else 6257 y_current->y_array[i] = array[i]; 6258 } 6259 } 6260 } 6261 else 6262 { 6263 /* Free array[] if it was filled. */ 6264 for (i = 0; i < size; i++) 6265 vim_free(array[i]); 6266 } 6267 vim_free(array); 6268 6269 return eof; 6270 } 6271 6272 /* 6273 * Accept a new style register line from the viminfo, store it when it's new. 6274 */ 6275 void 6276 handle_viminfo_register(garray_T *values, int force) 6277 { 6278 bval_T *vp = (bval_T *)values->ga_data; 6279 int flags; 6280 int name; 6281 int type; 6282 int linecount; 6283 int width; 6284 time_t timestamp; 6285 yankreg_T *y_ptr; 6286 int i; 6287 6288 /* Check the format: 6289 * |{bartype},{flags},{name},{type}, 6290 * {linecount},{width},{timestamp},"line1","line2" 6291 */ 6292 if (values->ga_len < 6 6293 || vp[0].bv_type != BVAL_NR 6294 || vp[1].bv_type != BVAL_NR 6295 || vp[2].bv_type != BVAL_NR 6296 || vp[3].bv_type != BVAL_NR 6297 || vp[4].bv_type != BVAL_NR 6298 || vp[5].bv_type != BVAL_NR) 6299 return; 6300 flags = vp[0].bv_nr; 6301 name = vp[1].bv_nr; 6302 if (name < 0 || name >= NUM_REGISTERS) 6303 return; 6304 type = vp[2].bv_nr; 6305 if (type != MCHAR && type != MLINE && type != MBLOCK) 6306 return; 6307 linecount = vp[3].bv_nr; 6308 if (values->ga_len < 6 + linecount) 6309 return; 6310 width = vp[4].bv_nr; 6311 if (width < 0) 6312 return; 6313 6314 if (y_read_regs != NULL) 6315 /* Reading viminfo for merging and writing. Store the register 6316 * content, don't update the current registers. */ 6317 y_ptr = &y_read_regs[name]; 6318 else 6319 y_ptr = &y_regs[name]; 6320 6321 /* Do not overwrite unless forced or the timestamp is newer. */ 6322 timestamp = (time_t)vp[5].bv_nr; 6323 if (y_ptr->y_array != NULL && !force 6324 && (timestamp == 0 || y_ptr->y_time_set > timestamp)) 6325 return; 6326 6327 if (y_ptr->y_array != NULL) 6328 for (i = 0; i < y_ptr->y_size; i++) 6329 vim_free(y_ptr->y_array[i]); 6330 vim_free(y_ptr->y_array); 6331 6332 if (y_read_regs == NULL) 6333 { 6334 if (flags & REG_PREVIOUS) 6335 y_previous = y_ptr; 6336 if ((flags & REG_EXEC) && (force || execreg_lastc == NUL)) 6337 execreg_lastc = get_register_name(name); 6338 } 6339 y_ptr->y_type = type; 6340 y_ptr->y_width = width; 6341 y_ptr->y_size = linecount; 6342 y_ptr->y_time_set = timestamp; 6343 if (linecount == 0) 6344 y_ptr->y_array = NULL; 6345 else 6346 { 6347 y_ptr->y_array = 6348 (char_u **)alloc((unsigned)(linecount * sizeof(char_u *))); 6349 for (i = 0; i < linecount; i++) 6350 { 6351 if (vp[i + 6].bv_allocated) 6352 { 6353 y_ptr->y_array[i] = vp[i + 6].bv_string; 6354 vp[i + 6].bv_string = NULL; 6355 } 6356 else 6357 y_ptr->y_array[i] = vim_strsave(vp[i + 6].bv_string); 6358 } 6359 } 6360 } 6361 6362 void 6363 write_viminfo_registers(FILE *fp) 6364 { 6365 int i, j; 6366 char_u *type; 6367 char_u c; 6368 int num_lines; 6369 int max_num_lines; 6370 int max_kbyte; 6371 long len; 6372 yankreg_T *y_ptr; 6373 6374 fputs(_("\n# Registers:\n"), fp); 6375 6376 /* Get '<' value, use old '"' value if '<' is not found. */ 6377 max_num_lines = get_viminfo_parameter('<'); 6378 if (max_num_lines < 0) 6379 max_num_lines = get_viminfo_parameter('"'); 6380 if (max_num_lines == 0) 6381 return; 6382 max_kbyte = get_viminfo_parameter('s'); 6383 if (max_kbyte == 0) 6384 return; 6385 6386 for (i = 0; i < NUM_REGISTERS; i++) 6387 { 6388 #ifdef FEAT_CLIPBOARD 6389 /* Skip '*'/'+' register, we don't want them back next time */ 6390 if (i == STAR_REGISTER || i == PLUS_REGISTER) 6391 continue; 6392 #endif 6393 #ifdef FEAT_DND 6394 /* Neither do we want the '~' register */ 6395 if (i == TILDE_REGISTER) 6396 continue; 6397 #endif 6398 /* When reading viminfo for merging and writing: Use the register from 6399 * viminfo if it's newer. */ 6400 if (y_read_regs != NULL 6401 && y_read_regs[i].y_array != NULL 6402 && (y_regs[i].y_array == NULL || 6403 y_read_regs[i].y_time_set > y_regs[i].y_time_set)) 6404 y_ptr = &y_read_regs[i]; 6405 else if (y_regs[i].y_array == NULL) 6406 continue; 6407 else 6408 y_ptr = &y_regs[i]; 6409 6410 /* Skip empty registers. */ 6411 num_lines = y_ptr->y_size; 6412 if (num_lines == 0 6413 || (num_lines == 1 && y_ptr->y_type == MCHAR 6414 && *y_ptr->y_array[0] == NUL)) 6415 continue; 6416 6417 if (max_kbyte > 0) 6418 { 6419 /* Skip register if there is more text than the maximum size. */ 6420 len = 0; 6421 for (j = 0; j < num_lines; j++) 6422 len += (long)STRLEN(y_ptr->y_array[j]) + 1L; 6423 if (len > (long)max_kbyte * 1024L) 6424 continue; 6425 } 6426 6427 switch (y_ptr->y_type) 6428 { 6429 case MLINE: 6430 type = (char_u *)"LINE"; 6431 break; 6432 case MCHAR: 6433 type = (char_u *)"CHAR"; 6434 break; 6435 case MBLOCK: 6436 type = (char_u *)"BLOCK"; 6437 break; 6438 default: 6439 sprintf((char *)IObuff, _("E574: Unknown register type %d"), 6440 y_ptr->y_type); 6441 emsg(IObuff); 6442 type = (char_u *)"LINE"; 6443 break; 6444 } 6445 if (y_previous == &y_regs[i]) 6446 fprintf(fp, "\""); 6447 c = get_register_name(i); 6448 fprintf(fp, "\"%c", c); 6449 if (c == execreg_lastc) 6450 fprintf(fp, "@"); 6451 fprintf(fp, "\t%s\t%d\n", type, (int)y_ptr->y_width); 6452 6453 /* If max_num_lines < 0, then we save ALL the lines in the register */ 6454 if (max_num_lines > 0 && num_lines > max_num_lines) 6455 num_lines = max_num_lines; 6456 for (j = 0; j < num_lines; j++) 6457 { 6458 putc('\t', fp); 6459 viminfo_writestring(fp, y_ptr->y_array[j]); 6460 } 6461 6462 { 6463 int flags = 0; 6464 int remaining; 6465 6466 /* New style with a bar line. Format: 6467 * |{bartype},{flags},{name},{type}, 6468 * {linecount},{width},{timestamp},"line1","line2" 6469 * flags: REG_PREVIOUS - register is y_previous 6470 * REG_EXEC - used for @@ 6471 */ 6472 if (y_previous == &y_regs[i]) 6473 flags |= REG_PREVIOUS; 6474 if (c == execreg_lastc) 6475 flags |= REG_EXEC; 6476 fprintf(fp, "|%d,%d,%d,%d,%d,%d,%ld", BARTYPE_REGISTER, flags, 6477 i, y_ptr->y_type, num_lines, (int)y_ptr->y_width, 6478 (long)y_ptr->y_time_set); 6479 /* 11 chars for type/flags/name/type, 3 * 20 for numbers */ 6480 remaining = LSIZE - 71; 6481 for (j = 0; j < num_lines; j++) 6482 { 6483 putc(',', fp); 6484 --remaining; 6485 remaining = barline_writestring(fp, y_ptr->y_array[j], 6486 remaining); 6487 } 6488 putc('\n', fp); 6489 } 6490 } 6491 } 6492 #endif /* FEAT_VIMINFO */ 6493 6494 #if defined(FEAT_CLIPBOARD) || defined(PROTO) 6495 /* 6496 * SELECTION / PRIMARY ('*') 6497 * 6498 * Text selection stuff that uses the GUI selection register '*'. When using a 6499 * GUI this may be text from another window, otherwise it is the last text we 6500 * had highlighted with VIsual mode. With mouse support, clicking the middle 6501 * button performs the paste, otherwise you will need to do <"*p>. " 6502 * If not under X, it is synonymous with the clipboard register '+'. 6503 * 6504 * X CLIPBOARD ('+') 6505 * 6506 * Text selection stuff that uses the GUI clipboard register '+'. 6507 * Under X, this matches the standard cut/paste buffer CLIPBOARD selection. 6508 * It will be used for unnamed cut/pasting is 'clipboard' contains "unnamed", 6509 * otherwise you will need to do <"+p>. " 6510 * If not under X, it is synonymous with the selection register '*'. 6511 */ 6512 6513 /* 6514 * Routine to export any final X selection we had to the environment 6515 * so that the text is still available after Vim has exited. X selections 6516 * only exist while the owning application exists, so we write to the 6517 * permanent (while X runs) store CUT_BUFFER0. 6518 * Dump the CLIPBOARD selection if we own it (it's logically the more 6519 * 'permanent' of the two), otherwise the PRIMARY one. 6520 * For now, use a hard-coded sanity limit of 1Mb of data. 6521 */ 6522 #if (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO) 6523 void 6524 x11_export_final_selection(void) 6525 { 6526 Display *dpy; 6527 char_u *str = NULL; 6528 long_u len = 0; 6529 int motion_type = -1; 6530 6531 # ifdef FEAT_GUI 6532 if (gui.in_use) 6533 dpy = X_DISPLAY; 6534 else 6535 # endif 6536 # ifdef FEAT_XCLIPBOARD 6537 dpy = xterm_dpy; 6538 # else 6539 return; 6540 # endif 6541 6542 /* Get selection to export */ 6543 if (clip_plus.owned) 6544 motion_type = clip_convert_selection(&str, &len, &clip_plus); 6545 else if (clip_star.owned) 6546 motion_type = clip_convert_selection(&str, &len, &clip_star); 6547 6548 /* Check it's OK */ 6549 if (dpy != NULL && str != NULL && motion_type >= 0 6550 && len < 1024*1024 && len > 0) 6551 { 6552 #ifdef FEAT_MBYTE 6553 int ok = TRUE; 6554 6555 /* The CUT_BUFFER0 is supposed to always contain latin1. Convert from 6556 * 'enc' when it is a multi-byte encoding. When 'enc' is an 8-bit 6557 * encoding conversion usually doesn't work, so keep the text as-is. 6558 */ 6559 if (has_mbyte) 6560 { 6561 vimconv_T vc; 6562 6563 vc.vc_type = CONV_NONE; 6564 if (convert_setup(&vc, p_enc, (char_u *)"latin1") == OK) 6565 { 6566 int intlen = len; 6567 char_u *conv_str; 6568 6569 vc.vc_fail = TRUE; 6570 conv_str = string_convert(&vc, str, &intlen); 6571 len = intlen; 6572 if (conv_str != NULL) 6573 { 6574 vim_free(str); 6575 str = conv_str; 6576 } 6577 else 6578 { 6579 ok = FALSE; 6580 } 6581 convert_setup(&vc, NULL, NULL); 6582 } 6583 else 6584 { 6585 ok = FALSE; 6586 } 6587 } 6588 6589 /* Do not store the string if conversion failed. Better to use any 6590 * other selection than garbled text. */ 6591 if (ok) 6592 #endif 6593 { 6594 XStoreBuffer(dpy, (char *)str, (int)len, 0); 6595 XFlush(dpy); 6596 } 6597 } 6598 6599 vim_free(str); 6600 } 6601 #endif 6602 6603 void 6604 clip_free_selection(VimClipboard *cbd) 6605 { 6606 yankreg_T *y_ptr = y_current; 6607 6608 if (cbd == &clip_plus) 6609 y_current = &y_regs[PLUS_REGISTER]; 6610 else 6611 y_current = &y_regs[STAR_REGISTER]; 6612 free_yank_all(); 6613 y_current->y_size = 0; 6614 y_current = y_ptr; 6615 } 6616 6617 /* 6618 * Get the selected text and put it in the gui selection register '*' or '+'. 6619 */ 6620 void 6621 clip_get_selection(VimClipboard *cbd) 6622 { 6623 yankreg_T *old_y_previous, *old_y_current; 6624 pos_T old_cursor; 6625 pos_T old_visual; 6626 int old_visual_mode; 6627 colnr_T old_curswant; 6628 int old_set_curswant; 6629 pos_T old_op_start, old_op_end; 6630 oparg_T oa; 6631 cmdarg_T ca; 6632 6633 if (cbd->owned) 6634 { 6635 if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) 6636 || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) 6637 return; 6638 6639 /* Get the text between clip_star.start & clip_star.end */ 6640 old_y_previous = y_previous; 6641 old_y_current = y_current; 6642 old_cursor = curwin->w_cursor; 6643 old_curswant = curwin->w_curswant; 6644 old_set_curswant = curwin->w_set_curswant; 6645 old_op_start = curbuf->b_op_start; 6646 old_op_end = curbuf->b_op_end; 6647 old_visual = VIsual; 6648 old_visual_mode = VIsual_mode; 6649 clear_oparg(&oa); 6650 oa.regname = (cbd == &clip_plus ? '+' : '*'); 6651 oa.op_type = OP_YANK; 6652 vim_memset(&ca, 0, sizeof(ca)); 6653 ca.oap = &oa; 6654 ca.cmdchar = 'y'; 6655 ca.count1 = 1; 6656 ca.retval = CA_NO_ADJ_OP_END; 6657 do_pending_operator(&ca, 0, TRUE); 6658 y_previous = old_y_previous; 6659 y_current = old_y_current; 6660 curwin->w_cursor = old_cursor; 6661 changed_cline_bef_curs(); /* need to update w_virtcol et al */ 6662 curwin->w_curswant = old_curswant; 6663 curwin->w_set_curswant = old_set_curswant; 6664 curbuf->b_op_start = old_op_start; 6665 curbuf->b_op_end = old_op_end; 6666 VIsual = old_visual; 6667 VIsual_mode = old_visual_mode; 6668 } 6669 else if (!is_clipboard_needs_update()) 6670 { 6671 clip_free_selection(cbd); 6672 6673 /* Try to get selected text from another window */ 6674 clip_gen_request_selection(cbd); 6675 } 6676 } 6677 6678 /* 6679 * Convert from the GUI selection string into the '*'/'+' register. 6680 */ 6681 void 6682 clip_yank_selection( 6683 int type, 6684 char_u *str, 6685 long len, 6686 VimClipboard *cbd) 6687 { 6688 yankreg_T *y_ptr; 6689 6690 if (cbd == &clip_plus) 6691 y_ptr = &y_regs[PLUS_REGISTER]; 6692 else 6693 y_ptr = &y_regs[STAR_REGISTER]; 6694 6695 clip_free_selection(cbd); 6696 6697 str_to_reg(y_ptr, type, str, len, 0L, FALSE); 6698 } 6699 6700 /* 6701 * Convert the '*'/'+' register into a GUI selection string returned in *str 6702 * with length *len. 6703 * Returns the motion type, or -1 for failure. 6704 */ 6705 int 6706 clip_convert_selection(char_u **str, long_u *len, VimClipboard *cbd) 6707 { 6708 char_u *p; 6709 int lnum; 6710 int i, j; 6711 int_u eolsize; 6712 yankreg_T *y_ptr; 6713 6714 if (cbd == &clip_plus) 6715 y_ptr = &y_regs[PLUS_REGISTER]; 6716 else 6717 y_ptr = &y_regs[STAR_REGISTER]; 6718 6719 #ifdef USE_CRNL 6720 eolsize = 2; 6721 #else 6722 eolsize = 1; 6723 #endif 6724 6725 *str = NULL; 6726 *len = 0; 6727 if (y_ptr->y_array == NULL) 6728 return -1; 6729 6730 for (i = 0; i < y_ptr->y_size; i++) 6731 *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; 6732 6733 /* 6734 * Don't want newline character at end of last line if we're in MCHAR mode. 6735 */ 6736 if (y_ptr->y_type == MCHAR && *len >= eolsize) 6737 *len -= eolsize; 6738 6739 p = *str = lalloc(*len + 1, TRUE); /* add one to avoid zero */ 6740 if (p == NULL) 6741 return -1; 6742 lnum = 0; 6743 for (i = 0, j = 0; i < (int)*len; i++, j++) 6744 { 6745 if (y_ptr->y_array[lnum][j] == '\n') 6746 p[i] = NUL; 6747 else if (y_ptr->y_array[lnum][j] == NUL) 6748 { 6749 #ifdef USE_CRNL 6750 p[i++] = '\r'; 6751 #endif 6752 #ifdef USE_CR 6753 p[i] = '\r'; 6754 #else 6755 p[i] = '\n'; 6756 #endif 6757 lnum++; 6758 j = -1; 6759 } 6760 else 6761 p[i] = y_ptr->y_array[lnum][j]; 6762 } 6763 return y_ptr->y_type; 6764 } 6765 6766 6767 /* 6768 * If we have written to a clipboard register, send the text to the clipboard. 6769 */ 6770 static void 6771 may_set_selection(void) 6772 { 6773 if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) 6774 { 6775 clip_own_selection(&clip_star); 6776 clip_gen_set_selection(&clip_star); 6777 } 6778 else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) 6779 { 6780 clip_own_selection(&clip_plus); 6781 clip_gen_set_selection(&clip_plus); 6782 } 6783 } 6784 6785 #endif /* FEAT_CLIPBOARD || PROTO */ 6786 6787 6788 #if defined(FEAT_DND) || defined(PROTO) 6789 /* 6790 * Replace the contents of the '~' register with str. 6791 */ 6792 void 6793 dnd_yank_drag_data(char_u *str, long len) 6794 { 6795 yankreg_T *curr; 6796 6797 curr = y_current; 6798 y_current = &y_regs[TILDE_REGISTER]; 6799 free_yank_all(); 6800 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE); 6801 y_current = curr; 6802 } 6803 #endif 6804 6805 6806 #if defined(FEAT_EVAL) || defined(PROTO) 6807 /* 6808 * Return the type of a register. 6809 * Used for getregtype() 6810 * Returns MAUTO for error. 6811 */ 6812 char_u 6813 get_reg_type(int regname, long *reglen) 6814 { 6815 switch (regname) 6816 { 6817 case '%': /* file name */ 6818 case '#': /* alternate file name */ 6819 case '=': /* expression */ 6820 case ':': /* last command line */ 6821 case '/': /* last search-pattern */ 6822 case '.': /* last inserted text */ 6823 #ifdef FEAT_SEARCHPATH 6824 case Ctrl_F: /* Filename under cursor */ 6825 case Ctrl_P: /* Path under cursor, expand via "path" */ 6826 #endif 6827 case Ctrl_W: /* word under cursor */ 6828 case Ctrl_A: /* WORD (mnemonic All) under cursor */ 6829 case '_': /* black hole: always empty */ 6830 return MCHAR; 6831 } 6832 6833 #ifdef FEAT_CLIPBOARD 6834 regname = may_get_selection(regname); 6835 #endif 6836 6837 if (regname != NUL && !valid_yank_reg(regname, FALSE)) 6838 return MAUTO; 6839 6840 get_yank_register(regname, FALSE); 6841 6842 if (y_current->y_array != NULL) 6843 { 6844 if (reglen != NULL && y_current->y_type == MBLOCK) 6845 *reglen = y_current->y_width; 6846 return y_current->y_type; 6847 } 6848 return MAUTO; 6849 } 6850 6851 static char_u *getreg_wrap_one_line(char_u *s, int flags); 6852 6853 /* 6854 * When "flags" has GREG_LIST return a list with text "s". 6855 * Otherwise just return "s". 6856 */ 6857 static char_u * 6858 getreg_wrap_one_line(char_u *s, int flags) 6859 { 6860 if (flags & GREG_LIST) 6861 { 6862 list_T *list = list_alloc(); 6863 6864 if (list != NULL) 6865 { 6866 if (list_append_string(list, NULL, -1) == FAIL) 6867 { 6868 list_free(list); 6869 return NULL; 6870 } 6871 list->lv_first->li_tv.vval.v_string = s; 6872 } 6873 return (char_u *)list; 6874 } 6875 return s; 6876 } 6877 6878 /* 6879 * Return the contents of a register as a single allocated string. 6880 * Used for "@r" in expressions and for getreg(). 6881 * Returns NULL for error. 6882 * Flags: 6883 * GREG_NO_EXPR Do not allow expression register 6884 * GREG_EXPR_SRC For the expression register: return expression itself, 6885 * not the result of its evaluation. 6886 * GREG_LIST Return a list of lines in place of a single string. 6887 */ 6888 char_u * 6889 get_reg_contents(int regname, int flags) 6890 { 6891 long i; 6892 char_u *retval; 6893 int allocated; 6894 long len; 6895 6896 /* Don't allow using an expression register inside an expression */ 6897 if (regname == '=') 6898 { 6899 if (flags & GREG_NO_EXPR) 6900 return NULL; 6901 if (flags & GREG_EXPR_SRC) 6902 return getreg_wrap_one_line(get_expr_line_src(), flags); 6903 return getreg_wrap_one_line(get_expr_line(), flags); 6904 } 6905 6906 if (regname == '@') /* "@@" is used for unnamed register */ 6907 regname = '"'; 6908 6909 /* check for valid regname */ 6910 if (regname != NUL && !valid_yank_reg(regname, FALSE)) 6911 return NULL; 6912 6913 #ifdef FEAT_CLIPBOARD 6914 regname = may_get_selection(regname); 6915 #endif 6916 6917 if (get_spec_reg(regname, &retval, &allocated, FALSE)) 6918 { 6919 if (retval == NULL) 6920 return NULL; 6921 if (allocated) 6922 return getreg_wrap_one_line(retval, flags); 6923 return getreg_wrap_one_line(vim_strsave(retval), flags); 6924 } 6925 6926 get_yank_register(regname, FALSE); 6927 if (y_current->y_array == NULL) 6928 return NULL; 6929 6930 if (flags & GREG_LIST) 6931 { 6932 list_T *list = list_alloc(); 6933 int error = FALSE; 6934 6935 if (list == NULL) 6936 return NULL; 6937 for (i = 0; i < y_current->y_size; ++i) 6938 if (list_append_string(list, y_current->y_array[i], -1) == FAIL) 6939 error = TRUE; 6940 if (error) 6941 { 6942 list_free(list); 6943 return NULL; 6944 } 6945 return (char_u *)list; 6946 } 6947 6948 /* 6949 * Compute length of resulting string. 6950 */ 6951 len = 0; 6952 for (i = 0; i < y_current->y_size; ++i) 6953 { 6954 len += (long)STRLEN(y_current->y_array[i]); 6955 /* 6956 * Insert a newline between lines and after last line if 6957 * y_type is MLINE. 6958 */ 6959 if (y_current->y_type == MLINE || i < y_current->y_size - 1) 6960 ++len; 6961 } 6962 6963 retval = lalloc(len + 1, TRUE); 6964 6965 /* 6966 * Copy the lines of the yank register into the string. 6967 */ 6968 if (retval != NULL) 6969 { 6970 len = 0; 6971 for (i = 0; i < y_current->y_size; ++i) 6972 { 6973 STRCPY(retval + len, y_current->y_array[i]); 6974 len += (long)STRLEN(retval + len); 6975 6976 /* 6977 * Insert a NL between lines and after the last line if y_type is 6978 * MLINE. 6979 */ 6980 if (y_current->y_type == MLINE || i < y_current->y_size - 1) 6981 retval[len++] = '\n'; 6982 } 6983 retval[len] = NUL; 6984 } 6985 6986 return retval; 6987 } 6988 6989 static int 6990 init_write_reg( 6991 int name, 6992 yankreg_T **old_y_previous, 6993 yankreg_T **old_y_current, 6994 int must_append, 6995 int *yank_type UNUSED) 6996 { 6997 if (!valid_yank_reg(name, TRUE)) /* check for valid reg name */ 6998 { 6999 emsg_invreg(name); 7000 return FAIL; 7001 } 7002 7003 /* Don't want to change the current (unnamed) register */ 7004 *old_y_previous = y_previous; 7005 *old_y_current = y_current; 7006 7007 get_yank_register(name, TRUE); 7008 if (!y_append && !must_append) 7009 free_yank_all(); 7010 return OK; 7011 } 7012 7013 static void 7014 finish_write_reg( 7015 int name, 7016 yankreg_T *old_y_previous, 7017 yankreg_T *old_y_current) 7018 { 7019 # ifdef FEAT_CLIPBOARD 7020 /* Send text of clipboard register to the clipboard. */ 7021 may_set_selection(); 7022 # endif 7023 7024 /* ':let @" = "val"' should change the meaning of the "" register */ 7025 if (name != '"') 7026 y_previous = old_y_previous; 7027 y_current = old_y_current; 7028 } 7029 7030 /* 7031 * Store string "str" in register "name". 7032 * "maxlen" is the maximum number of bytes to use, -1 for all bytes. 7033 * If "must_append" is TRUE, always append to the register. Otherwise append 7034 * if "name" is an uppercase letter. 7035 * Note: "maxlen" and "must_append" don't work for the "/" register. 7036 * Careful: 'str' is modified, you may have to use a copy! 7037 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. 7038 */ 7039 void 7040 write_reg_contents( 7041 int name, 7042 char_u *str, 7043 int maxlen, 7044 int must_append) 7045 { 7046 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); 7047 } 7048 7049 void 7050 write_reg_contents_lst( 7051 int name, 7052 char_u **strings, 7053 int maxlen UNUSED, 7054 int must_append, 7055 int yank_type, 7056 long block_len) 7057 { 7058 yankreg_T *old_y_previous, *old_y_current; 7059 7060 if (name == '/' 7061 #ifdef FEAT_EVAL 7062 || name == '=' 7063 #endif 7064 ) 7065 { 7066 char_u *s; 7067 7068 if (strings[0] == NULL) 7069 s = (char_u *)""; 7070 else if (strings[1] != NULL) 7071 { 7072 EMSG(_("E883: search pattern and expression register may not " 7073 "contain two or more lines")); 7074 return; 7075 } 7076 else 7077 s = strings[0]; 7078 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); 7079 return; 7080 } 7081 7082 if (name == '_') /* black hole: nothing to do */ 7083 return; 7084 7085 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, 7086 &yank_type) == FAIL) 7087 return; 7088 7089 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE); 7090 7091 finish_write_reg(name, old_y_previous, old_y_current); 7092 } 7093 7094 void 7095 write_reg_contents_ex( 7096 int name, 7097 char_u *str, 7098 int maxlen, 7099 int must_append, 7100 int yank_type, 7101 long block_len) 7102 { 7103 yankreg_T *old_y_previous, *old_y_current; 7104 long len; 7105 7106 if (maxlen >= 0) 7107 len = maxlen; 7108 else 7109 len = (long)STRLEN(str); 7110 7111 /* Special case: '/' search pattern */ 7112 if (name == '/') 7113 { 7114 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); 7115 return; 7116 } 7117 7118 if (name == '#') 7119 { 7120 buf_T *buf; 7121 7122 if (VIM_ISDIGIT(*str)) 7123 { 7124 int num = atoi((char *)str); 7125 7126 buf = buflist_findnr(num); 7127 if (buf == NULL) 7128 EMSGN(_(e_nobufnr), (long)num); 7129 } 7130 else 7131 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str), 7132 TRUE, FALSE, FALSE)); 7133 if (buf == NULL) 7134 return; 7135 curwin->w_alt_fnum = buf->b_fnum; 7136 return; 7137 } 7138 7139 #ifdef FEAT_EVAL 7140 if (name == '=') 7141 { 7142 char_u *p, *s; 7143 7144 p = vim_strnsave(str, (int)len); 7145 if (p == NULL) 7146 return; 7147 if (must_append) 7148 { 7149 s = concat_str(get_expr_line_src(), p); 7150 vim_free(p); 7151 p = s; 7152 } 7153 set_expr_line(p); 7154 return; 7155 } 7156 #endif 7157 7158 if (name == '_') /* black hole: nothing to do */ 7159 return; 7160 7161 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, 7162 &yank_type) == FAIL) 7163 return; 7164 7165 str_to_reg(y_current, yank_type, str, len, block_len, FALSE); 7166 7167 finish_write_reg(name, old_y_previous, old_y_current); 7168 } 7169 #endif /* FEAT_EVAL */ 7170 7171 #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) 7172 /* 7173 * Put a string into a register. When the register is not empty, the string 7174 * is appended. 7175 */ 7176 static void 7177 str_to_reg( 7178 yankreg_T *y_ptr, /* pointer to yank register */ 7179 int yank_type, /* MCHAR, MLINE, MBLOCK, MAUTO */ 7180 char_u *str, /* string to put in register */ 7181 long len, /* length of string */ 7182 long blocklen, /* width of Visual block */ 7183 int str_list) /* TRUE if str is char_u ** */ 7184 { 7185 int type; /* MCHAR, MLINE or MBLOCK */ 7186 int lnum; 7187 long start; 7188 long i; 7189 int extra; 7190 int newlines; /* number of lines added */ 7191 int extraline = 0; /* extra line at the end */ 7192 int append = FALSE; /* append to last line in register */ 7193 char_u *s; 7194 char_u **ss; 7195 char_u **pp; 7196 long maxlen; 7197 7198 if (y_ptr->y_array == NULL) /* NULL means empty register */ 7199 y_ptr->y_size = 0; 7200 7201 if (yank_type == MAUTO) 7202 type = ((str_list || (len > 0 && (str[len - 1] == NL 7203 || str[len - 1] == CAR))) 7204 ? MLINE : MCHAR); 7205 else 7206 type = yank_type; 7207 7208 /* 7209 * Count the number of lines within the string 7210 */ 7211 newlines = 0; 7212 if (str_list) 7213 { 7214 for (ss = (char_u **) str; *ss != NULL; ++ss) 7215 ++newlines; 7216 } 7217 else 7218 { 7219 for (i = 0; i < len; i++) 7220 if (str[i] == '\n') 7221 ++newlines; 7222 if (type == MCHAR || len == 0 || str[len - 1] != '\n') 7223 { 7224 extraline = 1; 7225 ++newlines; /* count extra newline at the end */ 7226 } 7227 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) 7228 { 7229 append = TRUE; 7230 --newlines; /* uncount newline when appending first line */ 7231 } 7232 } 7233 7234 /* Without any lines make the register empty. */ 7235 if (y_ptr->y_size + newlines == 0) 7236 { 7237 VIM_CLEAR(y_ptr->y_array); 7238 return; 7239 } 7240 7241 /* 7242 * Allocate an array to hold the pointers to the new register lines. 7243 * If the register was not empty, move the existing lines to the new array. 7244 */ 7245 pp = (char_u **)lalloc_clear((y_ptr->y_size + newlines) 7246 * sizeof(char_u *), TRUE); 7247 if (pp == NULL) /* out of memory */ 7248 return; 7249 for (lnum = 0; lnum < y_ptr->y_size; ++lnum) 7250 pp[lnum] = y_ptr->y_array[lnum]; 7251 vim_free(y_ptr->y_array); 7252 y_ptr->y_array = pp; 7253 maxlen = 0; 7254 7255 /* 7256 * Find the end of each line and save it into the array. 7257 */ 7258 if (str_list) 7259 { 7260 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) 7261 { 7262 i = (long)STRLEN(*ss); 7263 pp[lnum] = vim_strnsave(*ss, i); 7264 if (i > maxlen) 7265 maxlen = i; 7266 } 7267 } 7268 else 7269 { 7270 for (start = 0; start < len + extraline; start += i + 1) 7271 { 7272 for (i = start; i < len; ++i) /* find the end of the line */ 7273 if (str[i] == '\n') 7274 break; 7275 i -= start; /* i is now length of line */ 7276 if (i > maxlen) 7277 maxlen = i; 7278 if (append) 7279 { 7280 --lnum; 7281 extra = (int)STRLEN(y_ptr->y_array[lnum]); 7282 } 7283 else 7284 extra = 0; 7285 s = alloc((unsigned)(i + extra + 1)); 7286 if (s == NULL) 7287 break; 7288 if (extra) 7289 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); 7290 if (append) 7291 vim_free(y_ptr->y_array[lnum]); 7292 if (i) 7293 mch_memmove(s + extra, str + start, (size_t)i); 7294 extra += i; 7295 s[extra] = NUL; 7296 y_ptr->y_array[lnum++] = s; 7297 while (--extra >= 0) 7298 { 7299 if (*s == NUL) 7300 *s = '\n'; /* replace NUL with newline */ 7301 ++s; 7302 } 7303 append = FALSE; /* only first line is appended */ 7304 } 7305 } 7306 y_ptr->y_type = type; 7307 y_ptr->y_size = lnum; 7308 if (type == MBLOCK) 7309 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); 7310 else 7311 y_ptr->y_width = 0; 7312 #ifdef FEAT_VIMINFO 7313 y_ptr->y_time_set = vim_time(); 7314 #endif 7315 } 7316 #endif /* FEAT_CLIPBOARD || FEAT_EVAL || PROTO */ 7317 7318 void 7319 clear_oparg(oparg_T *oap) 7320 { 7321 vim_memset(oap, 0, sizeof(oparg_T)); 7322 } 7323 7324 static varnumber_T line_count_info(char_u *line, varnumber_T *wc, varnumber_T *cc, varnumber_T limit, int eol_size); 7325 7326 /* 7327 * Count the number of bytes, characters and "words" in a line. 7328 * 7329 * "Words" are counted by looking for boundaries between non-space and 7330 * space characters. (it seems to produce results that match 'wc'.) 7331 * 7332 * Return value is byte count; word count for the line is added to "*wc". 7333 * Char count is added to "*cc". 7334 * 7335 * The function will only examine the first "limit" characters in the 7336 * line, stopping if it encounters an end-of-line (NUL byte). In that 7337 * case, eol_size will be added to the character count to account for 7338 * the size of the EOL character. 7339 */ 7340 static varnumber_T 7341 line_count_info( 7342 char_u *line, 7343 varnumber_T *wc, 7344 varnumber_T *cc, 7345 varnumber_T limit, 7346 int eol_size) 7347 { 7348 varnumber_T i; 7349 varnumber_T words = 0; 7350 varnumber_T chars = 0; 7351 int is_word = 0; 7352 7353 for (i = 0; i < limit && line[i] != NUL; ) 7354 { 7355 if (is_word) 7356 { 7357 if (vim_isspace(line[i])) 7358 { 7359 words++; 7360 is_word = 0; 7361 } 7362 } 7363 else if (!vim_isspace(line[i])) 7364 is_word = 1; 7365 ++chars; 7366 #ifdef FEAT_MBYTE 7367 i += (*mb_ptr2len)(line + i); 7368 #else 7369 ++i; 7370 #endif 7371 } 7372 7373 if (is_word) 7374 words++; 7375 *wc += words; 7376 7377 /* Add eol_size if the end of line was reached before hitting limit. */ 7378 if (i < limit && line[i] == NUL) 7379 { 7380 i += eol_size; 7381 chars += eol_size; 7382 } 7383 *cc += chars; 7384 return i; 7385 } 7386 7387 /* 7388 * Give some info about the position of the cursor (for "g CTRL-G"). 7389 * In Visual mode, give some info about the selected region. (In this case, 7390 * the *_count_cursor variables store running totals for the selection.) 7391 * When "dict" is not NULL store the info there instead of showing it. 7392 */ 7393 void 7394 cursor_pos_info(dict_T *dict) 7395 { 7396 char_u *p; 7397 char_u buf1[50]; 7398 char_u buf2[40]; 7399 linenr_T lnum; 7400 varnumber_T byte_count = 0; 7401 #ifdef FEAT_MBYTE 7402 varnumber_T bom_count = 0; 7403 #endif 7404 varnumber_T byte_count_cursor = 0; 7405 varnumber_T char_count = 0; 7406 varnumber_T char_count_cursor = 0; 7407 varnumber_T word_count = 0; 7408 varnumber_T word_count_cursor = 0; 7409 int eol_size; 7410 varnumber_T last_check = 100000L; 7411 long line_count_selected = 0; 7412 pos_T min_pos, max_pos; 7413 oparg_T oparg; 7414 struct block_def bd; 7415 7416 /* 7417 * Compute the length of the file in characters. 7418 */ 7419 if (curbuf->b_ml.ml_flags & ML_EMPTY) 7420 { 7421 if (dict == NULL) 7422 { 7423 MSG(_(no_lines_msg)); 7424 return; 7425 } 7426 } 7427 else 7428 { 7429 if (get_fileformat(curbuf) == EOL_DOS) 7430 eol_size = 2; 7431 else 7432 eol_size = 1; 7433 7434 if (VIsual_active) 7435 { 7436 if (LT_POS(VIsual, curwin->w_cursor)) 7437 { 7438 min_pos = VIsual; 7439 max_pos = curwin->w_cursor; 7440 } 7441 else 7442 { 7443 min_pos = curwin->w_cursor; 7444 max_pos = VIsual; 7445 } 7446 if (*p_sel == 'e' && max_pos.col > 0) 7447 --max_pos.col; 7448 7449 if (VIsual_mode == Ctrl_V) 7450 { 7451 #ifdef FEAT_LINEBREAK 7452 char_u * saved_sbr = p_sbr; 7453 7454 /* Make 'sbr' empty for a moment to get the correct size. */ 7455 p_sbr = empty_option; 7456 #endif 7457 oparg.is_VIsual = 1; 7458 oparg.block_mode = TRUE; 7459 oparg.op_type = OP_NOP; 7460 getvcols(curwin, &min_pos, &max_pos, 7461 &oparg.start_vcol, &oparg.end_vcol); 7462 #ifdef FEAT_LINEBREAK 7463 p_sbr = saved_sbr; 7464 #endif 7465 if (curwin->w_curswant == MAXCOL) 7466 oparg.end_vcol = MAXCOL; 7467 /* Swap the start, end vcol if needed */ 7468 if (oparg.end_vcol < oparg.start_vcol) 7469 { 7470 oparg.end_vcol += oparg.start_vcol; 7471 oparg.start_vcol = oparg.end_vcol - oparg.start_vcol; 7472 oparg.end_vcol -= oparg.start_vcol; 7473 } 7474 } 7475 line_count_selected = max_pos.lnum - min_pos.lnum + 1; 7476 } 7477 7478 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) 7479 { 7480 /* Check for a CTRL-C every 100000 characters. */ 7481 if (byte_count > last_check) 7482 { 7483 ui_breakcheck(); 7484 if (got_int) 7485 return; 7486 last_check = byte_count + 100000L; 7487 } 7488 7489 /* Do extra processing for VIsual mode. */ 7490 if (VIsual_active 7491 && lnum >= min_pos.lnum && lnum <= max_pos.lnum) 7492 { 7493 char_u *s = NULL; 7494 long len = 0L; 7495 7496 switch (VIsual_mode) 7497 { 7498 case Ctrl_V: 7499 #ifdef FEAT_VIRTUALEDIT 7500 virtual_op = virtual_active(); 7501 #endif 7502 block_prep(&oparg, &bd, lnum, 0); 7503 #ifdef FEAT_VIRTUALEDIT 7504 virtual_op = MAYBE; 7505 #endif 7506 s = bd.textstart; 7507 len = (long)bd.textlen; 7508 break; 7509 case 'V': 7510 s = ml_get(lnum); 7511 len = MAXCOL; 7512 break; 7513 case 'v': 7514 { 7515 colnr_T start_col = (lnum == min_pos.lnum) 7516 ? min_pos.col : 0; 7517 colnr_T end_col = (lnum == max_pos.lnum) 7518 ? max_pos.col - start_col + 1 : MAXCOL; 7519 7520 s = ml_get(lnum) + start_col; 7521 len = end_col; 7522 } 7523 break; 7524 } 7525 if (s != NULL) 7526 { 7527 byte_count_cursor += line_count_info(s, &word_count_cursor, 7528 &char_count_cursor, len, eol_size); 7529 if (lnum == curbuf->b_ml.ml_line_count 7530 && !curbuf->b_p_eol 7531 && (curbuf->b_p_bin || !curbuf->b_p_fixeol) 7532 && (long)STRLEN(s) < len) 7533 byte_count_cursor -= eol_size; 7534 } 7535 } 7536 else 7537 { 7538 /* In non-visual mode, check for the line the cursor is on */ 7539 if (lnum == curwin->w_cursor.lnum) 7540 { 7541 word_count_cursor += word_count; 7542 char_count_cursor += char_count; 7543 byte_count_cursor = byte_count + 7544 line_count_info(ml_get(lnum), 7545 &word_count_cursor, &char_count_cursor, 7546 (varnumber_T)(curwin->w_cursor.col + 1), 7547 eol_size); 7548 } 7549 } 7550 /* Add to the running totals */ 7551 byte_count += line_count_info(ml_get(lnum), &word_count, 7552 &char_count, (varnumber_T)MAXCOL, 7553 eol_size); 7554 } 7555 7556 /* Correction for when last line doesn't have an EOL. */ 7557 if (!curbuf->b_p_eol && (curbuf->b_p_bin || !curbuf->b_p_fixeol)) 7558 byte_count -= eol_size; 7559 7560 if (dict == NULL) 7561 { 7562 if (VIsual_active) 7563 { 7564 if (VIsual_mode == Ctrl_V && curwin->w_curswant < MAXCOL) 7565 { 7566 getvcols(curwin, &min_pos, &max_pos, &min_pos.col, 7567 &max_pos.col); 7568 vim_snprintf((char *)buf1, sizeof(buf1), _("%ld Cols; "), 7569 (long)(oparg.end_vcol - oparg.start_vcol + 1)); 7570 } 7571 else 7572 buf1[0] = NUL; 7573 7574 if (char_count_cursor == byte_count_cursor 7575 && char_count == byte_count) 7576 vim_snprintf((char *)IObuff, IOSIZE, 7577 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Bytes"), 7578 buf1, line_count_selected, 7579 (long)curbuf->b_ml.ml_line_count, 7580 (long long)word_count_cursor, 7581 (long long)word_count, 7582 (long long)byte_count_cursor, 7583 (long long)byte_count); 7584 else 7585 vim_snprintf((char *)IObuff, IOSIZE, 7586 _("Selected %s%ld of %ld Lines; %lld of %lld Words; %lld of %lld Chars; %lld of %lld Bytes"), 7587 buf1, line_count_selected, 7588 (long)curbuf->b_ml.ml_line_count, 7589 (long long)word_count_cursor, 7590 (long long)word_count, 7591 (long long)char_count_cursor, 7592 (long long)char_count, 7593 (long long)byte_count_cursor, 7594 (long long)byte_count); 7595 } 7596 else 7597 { 7598 p = ml_get_curline(); 7599 validate_virtcol(); 7600 col_print(buf1, sizeof(buf1), (int)curwin->w_cursor.col + 1, 7601 (int)curwin->w_virtcol + 1); 7602 col_print(buf2, sizeof(buf2), (int)STRLEN(p), 7603 linetabsize(p)); 7604 7605 if (char_count_cursor == byte_count_cursor 7606 && char_count == byte_count) 7607 vim_snprintf((char *)IObuff, IOSIZE, 7608 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Byte %lld of %lld"), 7609 (char *)buf1, (char *)buf2, 7610 (long)curwin->w_cursor.lnum, 7611 (long)curbuf->b_ml.ml_line_count, 7612 (long long)word_count_cursor, (long long)word_count, 7613 (long long)byte_count_cursor, (long long)byte_count); 7614 else 7615 vim_snprintf((char *)IObuff, IOSIZE, 7616 _("Col %s of %s; Line %ld of %ld; Word %lld of %lld; Char %lld of %lld; Byte %lld of %lld"), 7617 (char *)buf1, (char *)buf2, 7618 (long)curwin->w_cursor.lnum, 7619 (long)curbuf->b_ml.ml_line_count, 7620 (long long)word_count_cursor, (long long)word_count, 7621 (long long)char_count_cursor, (long long)char_count, 7622 (long long)byte_count_cursor, (long long)byte_count); 7623 } 7624 } 7625 7626 #ifdef FEAT_MBYTE 7627 bom_count = bomb_size(); 7628 if (bom_count > 0) 7629 vim_snprintf((char *)IObuff + STRLEN(IObuff), IOSIZE, 7630 _("(+%lld for BOM)"), (long long)bom_count); 7631 #endif 7632 if (dict == NULL) 7633 { 7634 /* Don't shorten this message, the user asked for it. */ 7635 p = p_shm; 7636 p_shm = (char_u *)""; 7637 msg(IObuff); 7638 p_shm = p; 7639 } 7640 } 7641 #if defined(FEAT_EVAL) 7642 if (dict != NULL) 7643 { 7644 dict_add_nr_str(dict, "words", word_count, NULL); 7645 dict_add_nr_str(dict, "chars", char_count, NULL); 7646 dict_add_nr_str(dict, "bytes", byte_count 7647 # ifdef FEAT_MBYTE 7648 + bom_count 7649 # endif 7650 , NULL); 7651 dict_add_nr_str(dict, VIsual_active ? "visual_bytes" : "cursor_bytes", 7652 byte_count_cursor, NULL); 7653 dict_add_nr_str(dict, VIsual_active ? "visual_chars" : "cursor_chars", 7654 char_count_cursor, NULL); 7655 dict_add_nr_str(dict, VIsual_active ? "visual_words" : "cursor_words", 7656 word_count_cursor, NULL); 7657 } 7658 #endif 7659 } 7660