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