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