| Bram Moolenaar | 4aea03e | 2019-09-25 22:37:17 +0200 | [diff] [blame^] | 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 | * register.c: functions for managing registers |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * Registers: |
| 18 | * 0 = unnamed register, for normal yanks and puts |
| 19 | * 1..9 = registers '1' to '9', for deletes |
| 20 | * 10..35 = registers 'a' to 'z' ('A' to 'Z' for appending) |
| 21 | * 36 = delete register '-' |
| 22 | * 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined |
| 23 | * 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined |
| 24 | */ |
| 25 | static yankreg_T y_regs[NUM_REGISTERS]; |
| 26 | |
| 27 | static yankreg_T *y_current; // ptr to current yankreg |
| 28 | static int y_append; // TRUE when appending |
| 29 | static yankreg_T *y_previous = NULL; // ptr to last written yankreg |
| 30 | |
| 31 | static int stuff_yank(int, char_u *); |
| 32 | static void put_reedit_in_typebuf(int silent); |
| 33 | static int put_in_typebuf(char_u *s, int esc, int colon, |
| 34 | int silent); |
| 35 | static void free_yank_all(void); |
| 36 | static int yank_copy_line(struct block_def *bd, long y_idx); |
| 37 | #ifdef FEAT_CLIPBOARD |
| 38 | static void copy_yank_reg(yankreg_T *reg); |
| 39 | static void may_set_selection(void); |
| 40 | #endif |
| 41 | static void dis_msg(char_u *p, int skip_esc); |
| 42 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 43 | static void str_to_reg(yankreg_T *y_ptr, int yank_type, char_u *str, long len, long blocklen, int str_list); |
| 44 | #endif |
| 45 | |
| 46 | yankreg_T * |
| 47 | get_y_regs(void) |
| 48 | { |
| 49 | return y_regs; |
| 50 | } |
| 51 | |
| 52 | yankreg_T * |
| 53 | get_y_current(void) |
| 54 | { |
| 55 | return y_current; |
| 56 | } |
| 57 | |
| 58 | yankreg_T * |
| 59 | get_y_previous(void) |
| 60 | { |
| 61 | return y_previous; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | set_y_previous(yankreg_T *yreg) |
| 66 | { |
| 67 | y_previous = yreg; |
| 68 | } |
| 69 | |
| 70 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 71 | /* |
| 72 | * Keep the last expression line here, for repeating. |
| 73 | */ |
| 74 | static char_u *expr_line = NULL; |
| 75 | |
| 76 | /* |
| 77 | * Get an expression for the "\"=expr1" or "CTRL-R =expr1" |
| 78 | * Returns '=' when OK, NUL otherwise. |
| 79 | */ |
| 80 | int |
| 81 | get_expr_register(void) |
| 82 | { |
| 83 | char_u *new_line; |
| 84 | |
| 85 | new_line = getcmdline('=', 0L, 0, TRUE); |
| 86 | if (new_line == NULL) |
| 87 | return NUL; |
| 88 | if (*new_line == NUL) // use previous line |
| 89 | vim_free(new_line); |
| 90 | else |
| 91 | set_expr_line(new_line); |
| 92 | return '='; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Set the expression for the '=' register. |
| 97 | * Argument must be an allocated string. |
| 98 | */ |
| 99 | void |
| 100 | set_expr_line(char_u *new_line) |
| 101 | { |
| 102 | vim_free(expr_line); |
| 103 | expr_line = new_line; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Get the result of the '=' register expression. |
| 108 | * Returns a pointer to allocated memory, or NULL for failure. |
| 109 | */ |
| 110 | char_u * |
| 111 | get_expr_line(void) |
| 112 | { |
| 113 | char_u *expr_copy; |
| 114 | char_u *rv; |
| 115 | static int nested = 0; |
| 116 | |
| 117 | if (expr_line == NULL) |
| 118 | return NULL; |
| 119 | |
| 120 | // Make a copy of the expression, because evaluating it may cause it to be |
| 121 | // changed. |
| 122 | expr_copy = vim_strsave(expr_line); |
| 123 | if (expr_copy == NULL) |
| 124 | return NULL; |
| 125 | |
| 126 | // When we are invoked recursively limit the evaluation to 10 levels. |
| 127 | // Then return the string as-is. |
| 128 | if (nested >= 10) |
| 129 | return expr_copy; |
| 130 | |
| 131 | ++nested; |
| 132 | rv = eval_to_string(expr_copy, NULL, TRUE); |
| 133 | --nested; |
| 134 | vim_free(expr_copy); |
| 135 | return rv; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Get the '=' register expression itself, without evaluating it. |
| 140 | */ |
| 141 | static char_u * |
| 142 | get_expr_line_src(void) |
| 143 | { |
| 144 | if (expr_line == NULL) |
| 145 | return NULL; |
| 146 | return vim_strsave(expr_line); |
| 147 | } |
| 148 | #endif // FEAT_EVAL |
| 149 | |
| 150 | /* |
| 151 | * Check if 'regname' is a valid name of a yank register. |
| 152 | * Note: There is no check for 0 (default register), caller should do this |
| 153 | */ |
| 154 | int |
| 155 | valid_yank_reg( |
| 156 | int regname, |
| 157 | int writing) // if TRUE check for writable registers |
| 158 | { |
| 159 | if ( (regname > 0 && ASCII_ISALNUM(regname)) |
| 160 | || (!writing && vim_strchr((char_u *) |
| 161 | #ifdef FEAT_EVAL |
| 162 | "/.%:=" |
| 163 | #else |
| 164 | "/.%:" |
| 165 | #endif |
| 166 | , regname) != NULL) |
| 167 | || regname == '#' |
| 168 | || regname == '"' |
| 169 | || regname == '-' |
| 170 | || regname == '_' |
| 171 | #ifdef FEAT_CLIPBOARD |
| 172 | || regname == '*' |
| 173 | || regname == '+' |
| 174 | #endif |
| 175 | #ifdef FEAT_DND |
| 176 | || (!writing && regname == '~') |
| 177 | #endif |
| 178 | ) |
| 179 | return TRUE; |
| 180 | return FALSE; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Set y_current and y_append, according to the value of "regname". |
| 185 | * Cannot handle the '_' register. |
| 186 | * Must only be called with a valid register name! |
| 187 | * |
| 188 | * If regname is 0 and writing, use register 0 |
| 189 | * If regname is 0 and reading, use previous register |
| 190 | * |
| 191 | * Return TRUE when the register should be inserted literally (selection or |
| 192 | * clipboard). |
| 193 | */ |
| 194 | int |
| 195 | get_yank_register(int regname, int writing) |
| 196 | { |
| 197 | int i; |
| 198 | int ret = FALSE; |
| 199 | |
| 200 | y_append = FALSE; |
| 201 | if ((regname == 0 || regname == '"') && !writing && y_previous != NULL) |
| 202 | { |
| 203 | y_current = y_previous; |
| 204 | return ret; |
| 205 | } |
| 206 | i = regname; |
| 207 | if (VIM_ISDIGIT(i)) |
| 208 | i -= '0'; |
| 209 | else if (ASCII_ISLOWER(i)) |
| 210 | i = CharOrdLow(i) + 10; |
| 211 | else if (ASCII_ISUPPER(i)) |
| 212 | { |
| 213 | i = CharOrdUp(i) + 10; |
| 214 | y_append = TRUE; |
| 215 | } |
| 216 | else if (regname == '-') |
| 217 | i = DELETION_REGISTER; |
| 218 | #ifdef FEAT_CLIPBOARD |
| 219 | // When selection is not available, use register 0 instead of '*' |
| 220 | else if (clip_star.available && regname == '*') |
| 221 | { |
| 222 | i = STAR_REGISTER; |
| 223 | ret = TRUE; |
| 224 | } |
| 225 | // When clipboard is not available, use register 0 instead of '+' |
| 226 | else if (clip_plus.available && regname == '+') |
| 227 | { |
| 228 | i = PLUS_REGISTER; |
| 229 | ret = TRUE; |
| 230 | } |
| 231 | #endif |
| 232 | #ifdef FEAT_DND |
| 233 | else if (!writing && regname == '~') |
| 234 | i = TILDE_REGISTER; |
| 235 | #endif |
| 236 | else // not 0-9, a-z, A-Z or '-': use register 0 |
| 237 | i = 0; |
| 238 | y_current = &(y_regs[i]); |
| 239 | if (writing) // remember the register we write into for do_put() |
| 240 | y_previous = y_current; |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 245 | /* |
| 246 | * When "regname" is a clipboard register, obtain the selection. If it's not |
| 247 | * available return zero, otherwise return "regname". |
| 248 | */ |
| 249 | int |
| 250 | may_get_selection(int regname) |
| 251 | { |
| 252 | if (regname == '*') |
| 253 | { |
| 254 | if (!clip_star.available) |
| 255 | regname = 0; |
| 256 | else |
| 257 | clip_get_selection(&clip_star); |
| 258 | } |
| 259 | else if (regname == '+') |
| 260 | { |
| 261 | if (!clip_plus.available) |
| 262 | regname = 0; |
| 263 | else |
| 264 | clip_get_selection(&clip_plus); |
| 265 | } |
| 266 | return regname; |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | /* |
| 271 | * Obtain the contents of a "normal" register. The register is made empty. |
| 272 | * The returned pointer has allocated memory, use put_register() later. |
| 273 | */ |
| 274 | void * |
| 275 | get_register( |
| 276 | int name, |
| 277 | int copy) // make a copy, if FALSE make register empty. |
| 278 | { |
| 279 | yankreg_T *reg; |
| 280 | int i; |
| 281 | |
| 282 | #ifdef FEAT_CLIPBOARD |
| 283 | // When Visual area changed, may have to update selection. Obtain the |
| 284 | // selection too. |
| 285 | if (name == '*' && clip_star.available) |
| 286 | { |
| 287 | if (clip_isautosel_star()) |
| 288 | clip_update_selection(&clip_star); |
| 289 | may_get_selection(name); |
| 290 | } |
| 291 | if (name == '+' && clip_plus.available) |
| 292 | { |
| 293 | if (clip_isautosel_plus()) |
| 294 | clip_update_selection(&clip_plus); |
| 295 | may_get_selection(name); |
| 296 | } |
| 297 | #endif |
| 298 | |
| 299 | get_yank_register(name, 0); |
| 300 | reg = ALLOC_ONE(yankreg_T); |
| 301 | if (reg != NULL) |
| 302 | { |
| 303 | *reg = *y_current; |
| 304 | if (copy) |
| 305 | { |
| 306 | // If we run out of memory some or all of the lines are empty. |
| 307 | if (reg->y_size == 0) |
| 308 | reg->y_array = NULL; |
| 309 | else |
| 310 | reg->y_array = ALLOC_MULT(char_u *, reg->y_size); |
| 311 | if (reg->y_array != NULL) |
| 312 | { |
| 313 | for (i = 0; i < reg->y_size; ++i) |
| 314 | reg->y_array[i] = vim_strsave(y_current->y_array[i]); |
| 315 | } |
| 316 | } |
| 317 | else |
| 318 | y_current->y_array = NULL; |
| 319 | } |
| 320 | return (void *)reg; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Put "reg" into register "name". Free any previous contents and "reg". |
| 325 | */ |
| 326 | void |
| 327 | put_register(int name, void *reg) |
| 328 | { |
| 329 | get_yank_register(name, 0); |
| 330 | free_yank_all(); |
| 331 | *y_current = *(yankreg_T *)reg; |
| 332 | vim_free(reg); |
| 333 | |
| 334 | #ifdef FEAT_CLIPBOARD |
| 335 | // Send text written to clipboard register to the clipboard. |
| 336 | may_set_selection(); |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | #if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \ |
| 341 | || defined(PROTO) |
| 342 | void |
| 343 | free_register(void *reg) |
| 344 | { |
| 345 | yankreg_T tmp; |
| 346 | |
| 347 | tmp = *y_current; |
| 348 | *y_current = *(yankreg_T *)reg; |
| 349 | free_yank_all(); |
| 350 | vim_free(reg); |
| 351 | *y_current = tmp; |
| 352 | } |
| 353 | #endif |
| 354 | |
| 355 | #if defined(FEAT_MOUSE) || defined(PROTO) |
| 356 | /* |
| 357 | * return TRUE if the current yank register has type MLINE |
| 358 | */ |
| 359 | int |
| 360 | yank_register_mline(int regname) |
| 361 | { |
| 362 | if (regname != 0 && !valid_yank_reg(regname, FALSE)) |
| 363 | return FALSE; |
| 364 | if (regname == '_') // black hole is always empty |
| 365 | return FALSE; |
| 366 | get_yank_register(regname, FALSE); |
| 367 | return (y_current->y_type == MLINE); |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | /* |
| 372 | * Start or stop recording into a yank register. |
| 373 | * |
| 374 | * Return FAIL for failure, OK otherwise. |
| 375 | */ |
| 376 | int |
| 377 | do_record(int c) |
| 378 | { |
| 379 | char_u *p; |
| 380 | static int regname; |
| 381 | yankreg_T *old_y_previous, *old_y_current; |
| 382 | int retval; |
| 383 | |
| 384 | if (reg_recording == 0) // start recording |
| 385 | { |
| 386 | // registers 0-9, a-z and " are allowed |
| 387 | if (c < 0 || (!ASCII_ISALNUM(c) && c != '"')) |
| 388 | retval = FAIL; |
| 389 | else |
| 390 | { |
| 391 | reg_recording = c; |
| 392 | showmode(); |
| 393 | regname = c; |
| 394 | retval = OK; |
| 395 | } |
| 396 | } |
| 397 | else // stop recording |
| 398 | { |
| 399 | // Get the recorded key hits. K_SPECIAL and CSI will be escaped, this |
| 400 | // needs to be removed again to put it in a register. exec_reg then |
| 401 | // adds the escaping back later. |
| 402 | reg_recording = 0; |
| 403 | msg(""); |
| 404 | p = get_recorded(); |
| 405 | if (p == NULL) |
| 406 | retval = FAIL; |
| 407 | else |
| 408 | { |
| 409 | // Remove escaping for CSI and K_SPECIAL in multi-byte chars. |
| 410 | vim_unescape_csi(p); |
| 411 | |
| 412 | // We don't want to change the default register here, so save and |
| 413 | // restore the current register name. |
| 414 | old_y_previous = y_previous; |
| 415 | old_y_current = y_current; |
| 416 | |
| 417 | retval = stuff_yank(regname, p); |
| 418 | |
| 419 | y_previous = old_y_previous; |
| 420 | y_current = old_y_current; |
| 421 | } |
| 422 | } |
| 423 | return retval; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Stuff string "p" into yank register "regname" as a single line (append if |
| 428 | * uppercase). "p" must have been alloced. |
| 429 | * |
| 430 | * return FAIL for failure, OK otherwise |
| 431 | */ |
| 432 | static int |
| 433 | stuff_yank(int regname, char_u *p) |
| 434 | { |
| 435 | char_u *lp; |
| 436 | char_u **pp; |
| 437 | |
| 438 | // check for read-only register |
| 439 | if (regname != 0 && !valid_yank_reg(regname, TRUE)) |
| 440 | { |
| 441 | vim_free(p); |
| 442 | return FAIL; |
| 443 | } |
| 444 | if (regname == '_') // black hole: don't do anything |
| 445 | { |
| 446 | vim_free(p); |
| 447 | return OK; |
| 448 | } |
| 449 | get_yank_register(regname, TRUE); |
| 450 | if (y_append && y_current->y_array != NULL) |
| 451 | { |
| 452 | pp = &(y_current->y_array[y_current->y_size - 1]); |
| 453 | lp = alloc(STRLEN(*pp) + STRLEN(p) + 1); |
| 454 | if (lp == NULL) |
| 455 | { |
| 456 | vim_free(p); |
| 457 | return FAIL; |
| 458 | } |
| 459 | STRCPY(lp, *pp); |
| 460 | STRCAT(lp, p); |
| 461 | vim_free(p); |
| 462 | vim_free(*pp); |
| 463 | *pp = lp; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | free_yank_all(); |
| 468 | if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL) |
| 469 | { |
| 470 | vim_free(p); |
| 471 | return FAIL; |
| 472 | } |
| 473 | y_current->y_array[0] = p; |
| 474 | y_current->y_size = 1; |
| 475 | y_current->y_type = MCHAR; // used to be MLINE, why? |
| 476 | #ifdef FEAT_VIMINFO |
| 477 | y_current->y_time_set = vim_time(); |
| 478 | #endif |
| 479 | } |
| 480 | return OK; |
| 481 | } |
| 482 | |
| 483 | static int execreg_lastc = NUL; |
| 484 | |
| 485 | int |
| 486 | get_execreg_lastc(void) |
| 487 | { |
| 488 | return execreg_lastc; |
| 489 | } |
| 490 | |
| 491 | void |
| 492 | set_execreg_lastc(int lastc) |
| 493 | { |
| 494 | execreg_lastc = lastc; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Execute a yank register: copy it into the stuff buffer. |
| 499 | * |
| 500 | * Return FAIL for failure, OK otherwise. |
| 501 | */ |
| 502 | int |
| 503 | do_execreg( |
| 504 | int regname, |
| 505 | int colon, // insert ':' before each line |
| 506 | int addcr, // always add '\n' to end of line |
| 507 | int silent) // set "silent" flag in typeahead buffer |
| 508 | { |
| 509 | long i; |
| 510 | char_u *p; |
| 511 | int retval = OK; |
| 512 | int remap; |
| 513 | |
| 514 | // repeat previous one |
| 515 | if (regname == '@') |
| 516 | { |
| 517 | if (execreg_lastc == NUL) |
| 518 | { |
| 519 | emsg(_("E748: No previously used register")); |
| 520 | return FAIL; |
| 521 | } |
| 522 | regname = execreg_lastc; |
| 523 | } |
| 524 | // check for valid regname |
| 525 | if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE)) |
| 526 | { |
| 527 | emsg_invreg(regname); |
| 528 | return FAIL; |
| 529 | } |
| 530 | execreg_lastc = regname; |
| 531 | |
| 532 | #ifdef FEAT_CLIPBOARD |
| 533 | regname = may_get_selection(regname); |
| 534 | #endif |
| 535 | |
| 536 | // black hole: don't stuff anything |
| 537 | if (regname == '_') |
| 538 | return OK; |
| 539 | |
| 540 | // use last command line |
| 541 | if (regname == ':') |
| 542 | { |
| 543 | if (last_cmdline == NULL) |
| 544 | { |
| 545 | emsg(_(e_nolastcmd)); |
| 546 | return FAIL; |
| 547 | } |
| 548 | // don't keep the cmdline containing @: |
| 549 | VIM_CLEAR(new_last_cmdline); |
| 550 | // Escape all control characters with a CTRL-V |
| 551 | p = vim_strsave_escaped_ext(last_cmdline, |
| 552 | (char_u *)"\001\002\003\004\005\006\007" |
| 553 | "\010\011\012\013\014\015\016\017" |
| 554 | "\020\021\022\023\024\025\026\027" |
| 555 | "\030\031\032\033\034\035\036\037", |
| 556 | Ctrl_V, FALSE); |
| 557 | if (p != NULL) |
| 558 | { |
| 559 | // When in Visual mode "'<,'>" will be prepended to the command. |
| 560 | // Remove it when it's already there. |
| 561 | if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0) |
| 562 | retval = put_in_typebuf(p + 5, TRUE, TRUE, silent); |
| 563 | else |
| 564 | retval = put_in_typebuf(p, TRUE, TRUE, silent); |
| 565 | } |
| 566 | vim_free(p); |
| 567 | } |
| 568 | #ifdef FEAT_EVAL |
| 569 | else if (regname == '=') |
| 570 | { |
| 571 | p = get_expr_line(); |
| 572 | if (p == NULL) |
| 573 | return FAIL; |
| 574 | retval = put_in_typebuf(p, TRUE, colon, silent); |
| 575 | vim_free(p); |
| 576 | } |
| 577 | #endif |
| 578 | else if (regname == '.') // use last inserted text |
| 579 | { |
| 580 | p = get_last_insert_save(); |
| 581 | if (p == NULL) |
| 582 | { |
| 583 | emsg(_(e_noinstext)); |
| 584 | return FAIL; |
| 585 | } |
| 586 | retval = put_in_typebuf(p, FALSE, colon, silent); |
| 587 | vim_free(p); |
| 588 | } |
| 589 | else |
| 590 | { |
| 591 | get_yank_register(regname, FALSE); |
| 592 | if (y_current->y_array == NULL) |
| 593 | return FAIL; |
| 594 | |
| 595 | // Disallow remaping for ":@r". |
| 596 | remap = colon ? REMAP_NONE : REMAP_YES; |
| 597 | |
| 598 | // Insert lines into typeahead buffer, from last one to first one. |
| 599 | put_reedit_in_typebuf(silent); |
| 600 | for (i = y_current->y_size; --i >= 0; ) |
| 601 | { |
| 602 | char_u *escaped; |
| 603 | |
| 604 | // insert NL between lines and after last line if type is MLINE |
| 605 | if (y_current->y_type == MLINE || i < y_current->y_size - 1 |
| 606 | || addcr) |
| 607 | { |
| 608 | if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL) |
| 609 | return FAIL; |
| 610 | } |
| 611 | escaped = vim_strsave_escape_csi(y_current->y_array[i]); |
| 612 | if (escaped == NULL) |
| 613 | return FAIL; |
| 614 | retval = ins_typebuf(escaped, remap, 0, TRUE, silent); |
| 615 | vim_free(escaped); |
| 616 | if (retval == FAIL) |
| 617 | return FAIL; |
| 618 | if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent) |
| 619 | == FAIL) |
| 620 | return FAIL; |
| 621 | } |
| 622 | reg_executing = regname == 0 ? '"' : regname; // disable "q" command |
| 623 | } |
| 624 | return retval; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * If "restart_edit" is not zero, put it in the typeahead buffer, so that it's |
| 629 | * used only after other typeahead has been processed. |
| 630 | */ |
| 631 | static void |
| 632 | put_reedit_in_typebuf(int silent) |
| 633 | { |
| 634 | char_u buf[3]; |
| 635 | |
| 636 | if (restart_edit != NUL) |
| 637 | { |
| 638 | if (restart_edit == 'V') |
| 639 | { |
| 640 | buf[0] = 'g'; |
| 641 | buf[1] = 'R'; |
| 642 | buf[2] = NUL; |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | buf[0] = restart_edit == 'I' ? 'i' : restart_edit; |
| 647 | buf[1] = NUL; |
| 648 | } |
| 649 | if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK) |
| 650 | restart_edit = NUL; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Insert register contents "s" into the typeahead buffer, so that it will be |
| 656 | * executed again. |
| 657 | * When "esc" is TRUE it is to be taken literally: Escape CSI characters and |
| 658 | * no remapping. |
| 659 | */ |
| 660 | static int |
| 661 | put_in_typebuf( |
| 662 | char_u *s, |
| 663 | int esc, |
| 664 | int colon, // add ':' before the line |
| 665 | int silent) |
| 666 | { |
| 667 | int retval = OK; |
| 668 | |
| 669 | put_reedit_in_typebuf(silent); |
| 670 | if (colon) |
| 671 | retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent); |
| 672 | if (retval == OK) |
| 673 | { |
| 674 | char_u *p; |
| 675 | |
| 676 | if (esc) |
| 677 | p = vim_strsave_escape_csi(s); |
| 678 | else |
| 679 | p = s; |
| 680 | if (p == NULL) |
| 681 | retval = FAIL; |
| 682 | else |
| 683 | retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES, |
| 684 | 0, TRUE, silent); |
| 685 | if (esc) |
| 686 | vim_free(p); |
| 687 | } |
| 688 | if (colon && retval == OK) |
| 689 | retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent); |
| 690 | return retval; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Insert a yank register: copy it into the Read buffer. |
| 695 | * Used by CTRL-R command and middle mouse button in insert mode. |
| 696 | * |
| 697 | * return FAIL for failure, OK otherwise |
| 698 | */ |
| 699 | int |
| 700 | insert_reg( |
| 701 | int regname, |
| 702 | int literally_arg) // insert literally, not as if typed |
| 703 | { |
| 704 | long i; |
| 705 | int retval = OK; |
| 706 | char_u *arg; |
| 707 | int allocated; |
| 708 | int literally = literally_arg; |
| 709 | |
| 710 | // It is possible to get into an endless loop by having CTRL-R a in |
| 711 | // register a and then, in insert mode, doing CTRL-R a. |
| 712 | // If you hit CTRL-C, the loop will be broken here. |
| 713 | ui_breakcheck(); |
| 714 | if (got_int) |
| 715 | return FAIL; |
| 716 | |
| 717 | // check for valid regname |
| 718 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 719 | return FAIL; |
| 720 | |
| 721 | #ifdef FEAT_CLIPBOARD |
| 722 | regname = may_get_selection(regname); |
| 723 | #endif |
| 724 | |
| 725 | if (regname == '.') // insert last inserted text |
| 726 | retval = stuff_inserted(NUL, 1L, TRUE); |
| 727 | else if (get_spec_reg(regname, &arg, &allocated, TRUE)) |
| 728 | { |
| 729 | if (arg == NULL) |
| 730 | return FAIL; |
| 731 | stuffescaped(arg, literally); |
| 732 | if (allocated) |
| 733 | vim_free(arg); |
| 734 | } |
| 735 | else // name or number register |
| 736 | { |
| 737 | if (get_yank_register(regname, FALSE)) |
| 738 | literally = TRUE; |
| 739 | if (y_current->y_array == NULL) |
| 740 | retval = FAIL; |
| 741 | else |
| 742 | { |
| 743 | for (i = 0; i < y_current->y_size; ++i) |
| 744 | { |
| 745 | stuffescaped(y_current->y_array[i], literally); |
| 746 | // Insert a newline between lines and after last line if |
| 747 | // y_type is MLINE. |
| 748 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 749 | stuffcharReadbuff('\n'); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return retval; |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * If "regname" is a special register, return TRUE and store a pointer to its |
| 759 | * value in "argp". |
| 760 | */ |
| 761 | int |
| 762 | get_spec_reg( |
| 763 | int regname, |
| 764 | char_u **argp, |
| 765 | int *allocated, // return: TRUE when value was allocated |
| 766 | int errmsg) // give error message when failing |
| 767 | { |
| 768 | int cnt; |
| 769 | |
| 770 | *argp = NULL; |
| 771 | *allocated = FALSE; |
| 772 | switch (regname) |
| 773 | { |
| 774 | case '%': // file name |
| 775 | if (errmsg) |
| 776 | check_fname(); // will give emsg if not set |
| 777 | *argp = curbuf->b_fname; |
| 778 | return TRUE; |
| 779 | |
| 780 | case '#': // alternate file name |
| 781 | *argp = getaltfname(errmsg); // may give emsg if not set |
| 782 | return TRUE; |
| 783 | |
| 784 | #ifdef FEAT_EVAL |
| 785 | case '=': // result of expression |
| 786 | *argp = get_expr_line(); |
| 787 | *allocated = TRUE; |
| 788 | return TRUE; |
| 789 | #endif |
| 790 | |
| 791 | case ':': // last command line |
| 792 | if (last_cmdline == NULL && errmsg) |
| 793 | emsg(_(e_nolastcmd)); |
| 794 | *argp = last_cmdline; |
| 795 | return TRUE; |
| 796 | |
| 797 | case '/': // last search-pattern |
| 798 | if (last_search_pat() == NULL && errmsg) |
| 799 | emsg(_(e_noprevre)); |
| 800 | *argp = last_search_pat(); |
| 801 | return TRUE; |
| 802 | |
| 803 | case '.': // last inserted text |
| 804 | *argp = get_last_insert_save(); |
| 805 | *allocated = TRUE; |
| 806 | if (*argp == NULL && errmsg) |
| 807 | emsg(_(e_noinstext)); |
| 808 | return TRUE; |
| 809 | |
| 810 | #ifdef FEAT_SEARCHPATH |
| 811 | case Ctrl_F: // Filename under cursor |
| 812 | case Ctrl_P: // Path under cursor, expand via "path" |
| 813 | if (!errmsg) |
| 814 | return FALSE; |
| 815 | *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP |
| 816 | | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL); |
| 817 | *allocated = TRUE; |
| 818 | return TRUE; |
| 819 | #endif |
| 820 | |
| 821 | case Ctrl_W: // word under cursor |
| 822 | case Ctrl_A: // WORD (mnemonic All) under cursor |
| 823 | if (!errmsg) |
| 824 | return FALSE; |
| 825 | cnt = find_ident_under_cursor(argp, regname == Ctrl_W |
| 826 | ? (FIND_IDENT|FIND_STRING) : FIND_STRING); |
| 827 | *argp = cnt ? vim_strnsave(*argp, cnt) : NULL; |
| 828 | *allocated = TRUE; |
| 829 | return TRUE; |
| 830 | |
| 831 | case Ctrl_L: // Line under cursor |
| 832 | if (!errmsg) |
| 833 | return FALSE; |
| 834 | |
| 835 | *argp = ml_get_buf(curwin->w_buffer, |
| 836 | curwin->w_cursor.lnum, FALSE); |
| 837 | return TRUE; |
| 838 | |
| 839 | case '_': // black hole: always empty |
| 840 | *argp = (char_u *)""; |
| 841 | return TRUE; |
| 842 | } |
| 843 | |
| 844 | return FALSE; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Paste a yank register into the command line. |
| 849 | * Only for non-special registers. |
| 850 | * Used by CTRL-R command in command-line mode |
| 851 | * insert_reg() can't be used here, because special characters from the |
| 852 | * register contents will be interpreted as commands. |
| 853 | * |
| 854 | * return FAIL for failure, OK otherwise |
| 855 | */ |
| 856 | int |
| 857 | cmdline_paste_reg( |
| 858 | int regname, |
| 859 | int literally_arg, // Insert text literally instead of "as typed" |
| 860 | int remcr) // don't add CR characters |
| 861 | { |
| 862 | long i; |
| 863 | int literally = literally_arg; |
| 864 | |
| 865 | if (get_yank_register(regname, FALSE)) |
| 866 | literally = TRUE; |
| 867 | if (y_current->y_array == NULL) |
| 868 | return FAIL; |
| 869 | |
| 870 | for (i = 0; i < y_current->y_size; ++i) |
| 871 | { |
| 872 | cmdline_paste_str(y_current->y_array[i], literally); |
| 873 | |
| 874 | // Insert ^M between lines and after last line if type is MLINE. |
| 875 | // Don't do this when "remcr" is TRUE. |
| 876 | if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr) |
| 877 | cmdline_paste_str((char_u *)"\r", literally); |
| 878 | |
| 879 | // Check for CTRL-C, in case someone tries to paste a few thousand |
| 880 | // lines and gets bored. |
| 881 | ui_breakcheck(); |
| 882 | if (got_int) |
| 883 | return FAIL; |
| 884 | } |
| 885 | return OK; |
| 886 | } |
| 887 | |
| 888 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 889 | /* |
| 890 | * Adjust the register name pointed to with "rp" for the clipboard being |
| 891 | * used always and the clipboard being available. |
| 892 | */ |
| 893 | void |
| 894 | adjust_clip_reg(int *rp) |
| 895 | { |
| 896 | // If no reg. specified, and "unnamed" or "unnamedplus" is in 'clipboard', |
| 897 | // use '*' or '+' reg, respectively. "unnamedplus" prevails. |
| 898 | if (*rp == 0 && (clip_unnamed != 0 || clip_unnamed_saved != 0)) |
| 899 | { |
| 900 | if (clip_unnamed != 0) |
| 901 | *rp = ((clip_unnamed & CLIP_UNNAMED_PLUS) && clip_plus.available) |
| 902 | ? '+' : '*'; |
| 903 | else |
| 904 | *rp = ((clip_unnamed_saved & CLIP_UNNAMED_PLUS) && clip_plus.available) |
| 905 | ? '+' : '*'; |
| 906 | } |
| 907 | if (!clip_star.available && *rp == '*') |
| 908 | *rp = 0; |
| 909 | if (!clip_plus.available && *rp == '+') |
| 910 | *rp = 0; |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | /* |
| 915 | * Shift the delete registers: "9 is cleared, "8 becomes "9, etc. |
| 916 | */ |
| 917 | void |
| 918 | shift_delete_registers() |
| 919 | { |
| 920 | int n; |
| 921 | |
| 922 | y_current = &y_regs[9]; |
| 923 | free_yank_all(); // free register nine |
| 924 | for (n = 9; n > 1; --n) |
| 925 | y_regs[n] = y_regs[n - 1]; |
| 926 | y_current = &y_regs[1]; |
| 927 | if (!y_append) |
| 928 | y_previous = y_current; |
| 929 | y_regs[1].y_array = NULL; // set register one to empty |
| 930 | } |
| 931 | |
| 932 | #if defined(FEAT_EVAL) |
| 933 | void |
| 934 | yank_do_autocmd(oparg_T *oap, yankreg_T *reg) |
| 935 | { |
| 936 | static int recursive = FALSE; |
| 937 | dict_T *v_event; |
| 938 | list_T *list; |
| 939 | int n; |
| 940 | char_u buf[NUMBUFLEN + 2]; |
| 941 | long reglen = 0; |
| 942 | |
| 943 | if (recursive) |
| 944 | return; |
| 945 | |
| 946 | v_event = get_vim_var_dict(VV_EVENT); |
| 947 | |
| 948 | list = list_alloc(); |
| 949 | if (list == NULL) |
| 950 | return; |
| 951 | for (n = 0; n < reg->y_size; n++) |
| 952 | list_append_string(list, reg->y_array[n], -1); |
| 953 | list->lv_lock = VAR_FIXED; |
| 954 | dict_add_list(v_event, "regcontents", list); |
| 955 | |
| 956 | buf[0] = (char_u)oap->regname; |
| 957 | buf[1] = NUL; |
| 958 | dict_add_string(v_event, "regname", buf); |
| 959 | |
| 960 | buf[0] = get_op_char(oap->op_type); |
| 961 | buf[1] = get_extra_op_char(oap->op_type); |
| 962 | buf[2] = NUL; |
| 963 | dict_add_string(v_event, "operator", buf); |
| 964 | |
| 965 | buf[0] = NUL; |
| 966 | buf[1] = NUL; |
| 967 | switch (get_reg_type(oap->regname, ®len)) |
| 968 | { |
| 969 | case MLINE: buf[0] = 'V'; break; |
| 970 | case MCHAR: buf[0] = 'v'; break; |
| 971 | case MBLOCK: |
| 972 | vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V, |
| 973 | reglen + 1); |
| 974 | break; |
| 975 | } |
| 976 | dict_add_string(v_event, "regtype", buf); |
| 977 | |
| 978 | // Lock the dictionary and its keys |
| 979 | dict_set_items_ro(v_event); |
| 980 | |
| 981 | recursive = TRUE; |
| 982 | textlock++; |
| 983 | apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf); |
| 984 | textlock--; |
| 985 | recursive = FALSE; |
| 986 | |
| 987 | // Empty the dictionary, v:event is still valid |
| 988 | dict_free_contents(v_event); |
| 989 | hash_init(&v_event->dv_hashtab); |
| 990 | } |
| 991 | #endif |
| 992 | |
| 993 | /* |
| 994 | * set all the yank registers to empty (called from main()) |
| 995 | */ |
| 996 | void |
| 997 | init_yank(void) |
| 998 | { |
| 999 | int i; |
| 1000 | |
| 1001 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 1002 | y_regs[i].y_array = NULL; |
| 1003 | } |
| 1004 | |
| 1005 | #if defined(EXITFREE) || defined(PROTO) |
| 1006 | void |
| 1007 | clear_registers(void) |
| 1008 | { |
| 1009 | int i; |
| 1010 | |
| 1011 | for (i = 0; i < NUM_REGISTERS; ++i) |
| 1012 | { |
| 1013 | y_current = &y_regs[i]; |
| 1014 | if (y_current->y_array != NULL) |
| 1015 | free_yank_all(); |
| 1016 | } |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | /* |
| 1021 | * Free "n" lines from the current yank register. |
| 1022 | * Called for normal freeing and in case of error. |
| 1023 | */ |
| 1024 | static void |
| 1025 | free_yank(long n) |
| 1026 | { |
| 1027 | if (y_current->y_array != NULL) |
| 1028 | { |
| 1029 | long i; |
| 1030 | |
| 1031 | for (i = n; --i >= 0; ) |
| 1032 | { |
| 1033 | #ifdef AMIGA // only for very slow machines |
| 1034 | if ((i & 1023) == 1023) // this may take a while |
| 1035 | { |
| 1036 | // This message should never cause a hit-return message. |
| 1037 | // Overwrite this message with any next message. |
| 1038 | ++no_wait_return; |
| 1039 | smsg(_("freeing %ld lines"), i + 1); |
| 1040 | --no_wait_return; |
| 1041 | msg_didout = FALSE; |
| 1042 | msg_col = 0; |
| 1043 | } |
| 1044 | #endif |
| 1045 | vim_free(y_current->y_array[i]); |
| 1046 | } |
| 1047 | VIM_CLEAR(y_current->y_array); |
| 1048 | #ifdef AMIGA |
| 1049 | if (n >= 1000) |
| 1050 | msg(""); |
| 1051 | #endif |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | free_yank_all(void) |
| 1057 | { |
| 1058 | free_yank(y_current->y_size); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Yank the text between "oap->start" and "oap->end" into a yank register. |
| 1063 | * If we are to append (uppercase register), we first yank into a new yank |
| 1064 | * register and then concatenate the old and the new one (so we keep the old |
| 1065 | * one in case of out-of-memory). |
| 1066 | * |
| 1067 | * Return FAIL for failure, OK otherwise. |
| 1068 | */ |
| 1069 | int |
| 1070 | op_yank(oparg_T *oap, int deleting, int mess) |
| 1071 | { |
| 1072 | long y_idx; // index in y_array[] |
| 1073 | yankreg_T *curr; // copy of y_current |
| 1074 | yankreg_T newreg; // new yank register when appending |
| 1075 | char_u **new_ptr; |
| 1076 | linenr_T lnum; // current line number |
| 1077 | long j; |
| 1078 | int yanktype = oap->motion_type; |
| 1079 | long yanklines = oap->line_count; |
| 1080 | linenr_T yankendlnum = oap->end.lnum; |
| 1081 | char_u *p; |
| 1082 | char_u *pnew; |
| 1083 | struct block_def bd; |
| 1084 | #if defined(FEAT_CLIPBOARD) && defined(FEAT_X11) |
| 1085 | int did_star = FALSE; |
| 1086 | #endif |
| 1087 | |
| 1088 | // check for read-only register |
| 1089 | if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE)) |
| 1090 | { |
| 1091 | beep_flush(); |
| 1092 | return FAIL; |
| 1093 | } |
| 1094 | if (oap->regname == '_') // black hole: nothing to do |
| 1095 | return OK; |
| 1096 | |
| 1097 | #ifdef FEAT_CLIPBOARD |
| 1098 | if (!clip_star.available && oap->regname == '*') |
| 1099 | oap->regname = 0; |
| 1100 | else if (!clip_plus.available && oap->regname == '+') |
| 1101 | oap->regname = 0; |
| 1102 | #endif |
| 1103 | |
| 1104 | if (!deleting) // op_delete() already set y_current |
| 1105 | get_yank_register(oap->regname, TRUE); |
| 1106 | |
| 1107 | curr = y_current; |
| 1108 | // append to existing contents |
| 1109 | if (y_append && y_current->y_array != NULL) |
| 1110 | y_current = &newreg; |
| 1111 | else |
| 1112 | free_yank_all(); // free previously yanked lines |
| 1113 | |
| 1114 | // If the cursor was in column 1 before and after the movement, and the |
| 1115 | // operator is not inclusive, the yank is always linewise. |
| 1116 | if ( oap->motion_type == MCHAR |
| 1117 | && oap->start.col == 0 |
| 1118 | && !oap->inclusive |
| 1119 | && (!oap->is_VIsual || *p_sel == 'o') |
| 1120 | && !oap->block_mode |
| 1121 | && oap->end.col == 0 |
| 1122 | && yanklines > 1) |
| 1123 | { |
| 1124 | yanktype = MLINE; |
| 1125 | --yankendlnum; |
| 1126 | --yanklines; |
| 1127 | } |
| 1128 | |
| 1129 | y_current->y_size = yanklines; |
| 1130 | y_current->y_type = yanktype; // set the yank register type |
| 1131 | y_current->y_width = 0; |
| 1132 | y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE); |
| 1133 | if (y_current->y_array == NULL) |
| 1134 | { |
| 1135 | y_current = curr; |
| 1136 | return FAIL; |
| 1137 | } |
| 1138 | #ifdef FEAT_VIMINFO |
| 1139 | y_current->y_time_set = vim_time(); |
| 1140 | #endif |
| 1141 | |
| 1142 | y_idx = 0; |
| 1143 | lnum = oap->start.lnum; |
| 1144 | |
| 1145 | if (oap->block_mode) |
| 1146 | { |
| 1147 | // Visual block mode |
| 1148 | y_current->y_type = MBLOCK; // set the yank register type |
| 1149 | y_current->y_width = oap->end_vcol - oap->start_vcol; |
| 1150 | |
| 1151 | if (curwin->w_curswant == MAXCOL && y_current->y_width > 0) |
| 1152 | y_current->y_width--; |
| 1153 | } |
| 1154 | |
| 1155 | for ( ; lnum <= yankendlnum; lnum++, y_idx++) |
| 1156 | { |
| 1157 | switch (y_current->y_type) |
| 1158 | { |
| 1159 | case MBLOCK: |
| 1160 | block_prep(oap, &bd, lnum, FALSE); |
| 1161 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 1162 | goto fail; |
| 1163 | break; |
| 1164 | |
| 1165 | case MLINE: |
| 1166 | if ((y_current->y_array[y_idx] = |
| 1167 | vim_strsave(ml_get(lnum))) == NULL) |
| 1168 | goto fail; |
| 1169 | break; |
| 1170 | |
| 1171 | case MCHAR: |
| 1172 | { |
| 1173 | colnr_T startcol = 0, endcol = MAXCOL; |
| 1174 | int is_oneChar = FALSE; |
| 1175 | colnr_T cs, ce; |
| 1176 | |
| 1177 | p = ml_get(lnum); |
| 1178 | bd.startspaces = 0; |
| 1179 | bd.endspaces = 0; |
| 1180 | |
| 1181 | if (lnum == oap->start.lnum) |
| 1182 | { |
| 1183 | startcol = oap->start.col; |
| 1184 | if (virtual_op) |
| 1185 | { |
| 1186 | getvcol(curwin, &oap->start, &cs, NULL, &ce); |
| 1187 | if (ce != cs && oap->start.coladd > 0) |
| 1188 | { |
| 1189 | // Part of a tab selected -- but don't |
| 1190 | // double-count it. |
| 1191 | bd.startspaces = (ce - cs + 1) |
| 1192 | - oap->start.coladd; |
| 1193 | startcol++; |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | if (lnum == oap->end.lnum) |
| 1199 | { |
| 1200 | endcol = oap->end.col; |
| 1201 | if (virtual_op) |
| 1202 | { |
| 1203 | getvcol(curwin, &oap->end, &cs, NULL, &ce); |
| 1204 | if (p[endcol] == NUL || (cs + oap->end.coladd < ce |
| 1205 | // Don't add space for double-wide |
| 1206 | // char; endcol will be on last byte |
| 1207 | // of multi-byte char. |
| 1208 | && (*mb_head_off)(p, p + endcol) == 0)) |
| 1209 | { |
| 1210 | if (oap->start.lnum == oap->end.lnum |
| 1211 | && oap->start.col == oap->end.col) |
| 1212 | { |
| 1213 | // Special case: inside a single char |
| 1214 | is_oneChar = TRUE; |
| 1215 | bd.startspaces = oap->end.coladd |
| 1216 | - oap->start.coladd + oap->inclusive; |
| 1217 | endcol = startcol; |
| 1218 | } |
| 1219 | else |
| 1220 | { |
| 1221 | bd.endspaces = oap->end.coladd |
| 1222 | + oap->inclusive; |
| 1223 | endcol -= oap->inclusive; |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | if (endcol == MAXCOL) |
| 1229 | endcol = (colnr_T)STRLEN(p); |
| 1230 | if (startcol > endcol || is_oneChar) |
| 1231 | bd.textlen = 0; |
| 1232 | else |
| 1233 | bd.textlen = endcol - startcol + oap->inclusive; |
| 1234 | bd.textstart = p + startcol; |
| 1235 | if (yank_copy_line(&bd, y_idx) == FAIL) |
| 1236 | goto fail; |
| 1237 | break; |
| 1238 | } |
| 1239 | // NOTREACHED |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if (curr != y_current) // append the new block to the old block |
| 1244 | { |
| 1245 | new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size); |
| 1246 | if (new_ptr == NULL) |
| 1247 | goto fail; |
| 1248 | for (j = 0; j < curr->y_size; ++j) |
| 1249 | new_ptr[j] = curr->y_array[j]; |
| 1250 | vim_free(curr->y_array); |
| 1251 | curr->y_array = new_ptr; |
| 1252 | #ifdef FEAT_VIMINFO |
| 1253 | curr->y_time_set = vim_time(); |
| 1254 | #endif |
| 1255 | |
| 1256 | if (yanktype == MLINE) // MLINE overrides MCHAR and MBLOCK |
| 1257 | curr->y_type = MLINE; |
| 1258 | |
| 1259 | // Concatenate the last line of the old block with the first line of |
| 1260 | // the new block, unless being Vi compatible. |
| 1261 | if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL) |
| 1262 | { |
| 1263 | pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1]) |
| 1264 | + STRLEN(y_current->y_array[0]) + 1); |
| 1265 | if (pnew == NULL) |
| 1266 | { |
| 1267 | y_idx = y_current->y_size - 1; |
| 1268 | goto fail; |
| 1269 | } |
| 1270 | STRCPY(pnew, curr->y_array[--j]); |
| 1271 | STRCAT(pnew, y_current->y_array[0]); |
| 1272 | vim_free(curr->y_array[j]); |
| 1273 | vim_free(y_current->y_array[0]); |
| 1274 | curr->y_array[j++] = pnew; |
| 1275 | y_idx = 1; |
| 1276 | } |
| 1277 | else |
| 1278 | y_idx = 0; |
| 1279 | while (y_idx < y_current->y_size) |
| 1280 | curr->y_array[j++] = y_current->y_array[y_idx++]; |
| 1281 | curr->y_size = j; |
| 1282 | vim_free(y_current->y_array); |
| 1283 | y_current = curr; |
| 1284 | } |
| 1285 | if (curwin->w_p_rnu) |
| 1286 | redraw_later(SOME_VALID); // cursor moved to start |
| 1287 | if (mess) // Display message about yank? |
| 1288 | { |
| 1289 | if (yanktype == MCHAR |
| 1290 | && !oap->block_mode |
| 1291 | && yanklines == 1) |
| 1292 | yanklines = 0; |
| 1293 | // Some versions of Vi use ">=" here, some don't... |
| 1294 | if (yanklines > p_report) |
| 1295 | { |
| 1296 | char namebuf[100]; |
| 1297 | |
| 1298 | if (oap->regname == NUL) |
| 1299 | *namebuf = NUL; |
| 1300 | else |
| 1301 | vim_snprintf(namebuf, sizeof(namebuf), |
| 1302 | _(" into \"%c"), oap->regname); |
| 1303 | |
| 1304 | // redisplay now, so message is not deleted |
| 1305 | update_topline_redraw(); |
| 1306 | if (oap->block_mode) |
| 1307 | { |
| 1308 | smsg(NGETTEXT("block of %ld line yanked%s", |
| 1309 | "block of %ld lines yanked%s", yanklines), |
| 1310 | yanklines, namebuf); |
| 1311 | } |
| 1312 | else |
| 1313 | { |
| 1314 | smsg(NGETTEXT("%ld line yanked%s", |
| 1315 | "%ld lines yanked%s", yanklines), |
| 1316 | yanklines, namebuf); |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | // Set "'[" and "']" marks. |
| 1322 | curbuf->b_op_start = oap->start; |
| 1323 | curbuf->b_op_end = oap->end; |
| 1324 | if (yanktype == MLINE && !oap->block_mode) |
| 1325 | { |
| 1326 | curbuf->b_op_start.col = 0; |
| 1327 | curbuf->b_op_end.col = MAXCOL; |
| 1328 | } |
| 1329 | |
| 1330 | #ifdef FEAT_CLIPBOARD |
| 1331 | // If we were yanking to the '*' register, send result to clipboard. |
| 1332 | // If no register was specified, and "unnamed" in 'clipboard', make a copy |
| 1333 | // to the '*' register. |
| 1334 | if (clip_star.available |
| 1335 | && (curr == &(y_regs[STAR_REGISTER]) |
| 1336 | || (!deleting && oap->regname == 0 |
| 1337 | && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED)))) |
| 1338 | { |
| 1339 | if (curr != &(y_regs[STAR_REGISTER])) |
| 1340 | // Copy the text from register 0 to the clipboard register. |
| 1341 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 1342 | |
| 1343 | clip_own_selection(&clip_star); |
| 1344 | clip_gen_set_selection(&clip_star); |
| 1345 | # ifdef FEAT_X11 |
| 1346 | did_star = TRUE; |
| 1347 | # endif |
| 1348 | } |
| 1349 | |
| 1350 | # ifdef FEAT_X11 |
| 1351 | // If we were yanking to the '+' register, send result to selection. |
| 1352 | // Also copy to the '*' register, in case auto-select is off. |
| 1353 | if (clip_plus.available |
| 1354 | && (curr == &(y_regs[PLUS_REGISTER]) |
| 1355 | || (!deleting && oap->regname == 0 |
| 1356 | && ((clip_unnamed | clip_unnamed_saved) & |
| 1357 | CLIP_UNNAMED_PLUS)))) |
| 1358 | { |
| 1359 | if (curr != &(y_regs[PLUS_REGISTER])) |
| 1360 | // Copy the text from register 0 to the clipboard register. |
| 1361 | copy_yank_reg(&(y_regs[PLUS_REGISTER])); |
| 1362 | |
| 1363 | clip_own_selection(&clip_plus); |
| 1364 | clip_gen_set_selection(&clip_plus); |
| 1365 | if (!clip_isautosel_star() && !clip_isautosel_plus() |
| 1366 | && !did_star && curr == &(y_regs[PLUS_REGISTER])) |
| 1367 | { |
| 1368 | copy_yank_reg(&(y_regs[STAR_REGISTER])); |
| 1369 | clip_own_selection(&clip_star); |
| 1370 | clip_gen_set_selection(&clip_star); |
| 1371 | } |
| 1372 | } |
| 1373 | # endif |
| 1374 | #endif |
| 1375 | |
| 1376 | #if defined(FEAT_EVAL) |
| 1377 | if (!deleting && has_textyankpost()) |
| 1378 | yank_do_autocmd(oap, y_current); |
| 1379 | #endif |
| 1380 | |
| 1381 | return OK; |
| 1382 | |
| 1383 | fail: // free the allocated lines |
| 1384 | free_yank(y_idx + 1); |
| 1385 | y_current = curr; |
| 1386 | return FAIL; |
| 1387 | } |
| 1388 | |
| 1389 | static int |
| 1390 | yank_copy_line(struct block_def *bd, long y_idx) |
| 1391 | { |
| 1392 | char_u *pnew; |
| 1393 | |
| 1394 | if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1)) |
| 1395 | == NULL) |
| 1396 | return FAIL; |
| 1397 | y_current->y_array[y_idx] = pnew; |
| 1398 | vim_memset(pnew, ' ', (size_t)bd->startspaces); |
| 1399 | pnew += bd->startspaces; |
| 1400 | mch_memmove(pnew, bd->textstart, (size_t)bd->textlen); |
| 1401 | pnew += bd->textlen; |
| 1402 | vim_memset(pnew, ' ', (size_t)bd->endspaces); |
| 1403 | pnew += bd->endspaces; |
| 1404 | *pnew = NUL; |
| 1405 | return OK; |
| 1406 | } |
| 1407 | |
| 1408 | #ifdef FEAT_CLIPBOARD |
| 1409 | /* |
| 1410 | * Make a copy of the y_current register to register "reg". |
| 1411 | */ |
| 1412 | static void |
| 1413 | copy_yank_reg(yankreg_T *reg) |
| 1414 | { |
| 1415 | yankreg_T *curr = y_current; |
| 1416 | long j; |
| 1417 | |
| 1418 | y_current = reg; |
| 1419 | free_yank_all(); |
| 1420 | *y_current = *curr; |
| 1421 | y_current->y_array = lalloc_clear( |
| 1422 | sizeof(char_u *) * y_current->y_size, TRUE); |
| 1423 | if (y_current->y_array == NULL) |
| 1424 | y_current->y_size = 0; |
| 1425 | else |
| 1426 | for (j = 0; j < y_current->y_size; ++j) |
| 1427 | if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL) |
| 1428 | { |
| 1429 | free_yank(j); |
| 1430 | y_current->y_size = 0; |
| 1431 | break; |
| 1432 | } |
| 1433 | y_current = curr; |
| 1434 | } |
| 1435 | #endif |
| 1436 | |
| 1437 | /* |
| 1438 | * Put contents of register "regname" into the text. |
| 1439 | * Caller must check "regname" to be valid! |
| 1440 | * "flags": PUT_FIXINDENT make indent look nice |
| 1441 | * PUT_CURSEND leave cursor after end of new text |
| 1442 | * PUT_LINE force linewise put (":put") |
| 1443 | */ |
| 1444 | void |
| 1445 | do_put( |
| 1446 | int regname, |
| 1447 | int dir, // BACKWARD for 'P', FORWARD for 'p' |
| 1448 | long count, |
| 1449 | int flags) |
| 1450 | { |
| 1451 | char_u *ptr; |
| 1452 | char_u *newp, *oldp; |
| 1453 | int yanklen; |
| 1454 | int totlen = 0; // init for gcc |
| 1455 | linenr_T lnum; |
| 1456 | colnr_T col; |
| 1457 | long i; // index in y_array[] |
| 1458 | int y_type; |
| 1459 | long y_size; |
| 1460 | int oldlen; |
| 1461 | long y_width = 0; |
| 1462 | colnr_T vcol; |
| 1463 | int delcount; |
| 1464 | int incr = 0; |
| 1465 | long j; |
| 1466 | struct block_def bd; |
| 1467 | char_u **y_array = NULL; |
| 1468 | long nr_lines = 0; |
| 1469 | pos_T new_cursor; |
| 1470 | int indent; |
| 1471 | int orig_indent = 0; // init for gcc |
| 1472 | int indent_diff = 0; // init for gcc |
| 1473 | int first_indent = TRUE; |
| 1474 | int lendiff = 0; |
| 1475 | pos_T old_pos; |
| 1476 | char_u *insert_string = NULL; |
| 1477 | int allocated = FALSE; |
| 1478 | long cnt; |
| 1479 | |
| 1480 | #ifdef FEAT_CLIPBOARD |
| 1481 | // Adjust register name for "unnamed" in 'clipboard'. |
| 1482 | adjust_clip_reg(®name); |
| 1483 | (void)may_get_selection(regname); |
| 1484 | #endif |
| 1485 | |
| 1486 | if (flags & PUT_FIXINDENT) |
| 1487 | orig_indent = get_indent(); |
| 1488 | |
| 1489 | curbuf->b_op_start = curwin->w_cursor; // default for '[ mark |
| 1490 | curbuf->b_op_end = curwin->w_cursor; // default for '] mark |
| 1491 | |
| 1492 | // Using inserted text works differently, because the register includes |
| 1493 | // special characters (newlines, etc.). |
| 1494 | if (regname == '.') |
| 1495 | { |
| 1496 | if (VIsual_active) |
| 1497 | stuffcharReadbuff(VIsual_mode); |
| 1498 | (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') : |
| 1499 | (count == -1 ? 'O' : 'i')), count, FALSE); |
| 1500 | // Putting the text is done later, so can't really move the cursor to |
| 1501 | // the next character. Use "l" to simulate it. |
| 1502 | if ((flags & PUT_CURSEND) && gchar_cursor() != NUL) |
| 1503 | stuffcharReadbuff('l'); |
| 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | // For special registers '%' (file name), '#' (alternate file name) and |
| 1508 | // ':' (last command line), etc. we have to create a fake yank register. |
| 1509 | if (get_spec_reg(regname, &insert_string, &allocated, TRUE)) |
| 1510 | { |
| 1511 | if (insert_string == NULL) |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | // Autocommands may be executed when saving lines for undo. This might |
| 1516 | // make "y_array" invalid, so we start undo now to avoid that. |
| 1517 | if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL) |
| 1518 | goto end; |
| 1519 | |
| 1520 | if (insert_string != NULL) |
| 1521 | { |
| 1522 | y_type = MCHAR; |
| 1523 | #ifdef FEAT_EVAL |
| 1524 | if (regname == '=') |
| 1525 | { |
| 1526 | // For the = register we need to split the string at NL |
| 1527 | // characters. |
| 1528 | // Loop twice: count the number of lines and save them. |
| 1529 | for (;;) |
| 1530 | { |
| 1531 | y_size = 0; |
| 1532 | ptr = insert_string; |
| 1533 | while (ptr != NULL) |
| 1534 | { |
| 1535 | if (y_array != NULL) |
| 1536 | y_array[y_size] = ptr; |
| 1537 | ++y_size; |
| 1538 | ptr = vim_strchr(ptr, '\n'); |
| 1539 | if (ptr != NULL) |
| 1540 | { |
| 1541 | if (y_array != NULL) |
| 1542 | *ptr = NUL; |
| 1543 | ++ptr; |
| 1544 | // A trailing '\n' makes the register linewise. |
| 1545 | if (*ptr == NUL) |
| 1546 | { |
| 1547 | y_type = MLINE; |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | if (y_array != NULL) |
| 1553 | break; |
| 1554 | y_array = ALLOC_MULT(char_u *, y_size); |
| 1555 | if (y_array == NULL) |
| 1556 | goto end; |
| 1557 | } |
| 1558 | } |
| 1559 | else |
| 1560 | #endif |
| 1561 | { |
| 1562 | y_size = 1; // use fake one-line yank register |
| 1563 | y_array = &insert_string; |
| 1564 | } |
| 1565 | } |
| 1566 | else |
| 1567 | { |
| 1568 | get_yank_register(regname, FALSE); |
| 1569 | |
| 1570 | y_type = y_current->y_type; |
| 1571 | y_width = y_current->y_width; |
| 1572 | y_size = y_current->y_size; |
| 1573 | y_array = y_current->y_array; |
| 1574 | } |
| 1575 | |
| 1576 | if (y_type == MLINE) |
| 1577 | { |
| 1578 | if (flags & PUT_LINE_SPLIT) |
| 1579 | { |
| 1580 | char_u *p; |
| 1581 | |
| 1582 | // "p" or "P" in Visual mode: split the lines to put the text in |
| 1583 | // between. |
| 1584 | if (u_save_cursor() == FAIL) |
| 1585 | goto end; |
| 1586 | p = ml_get_cursor(); |
| 1587 | if (dir == FORWARD && *p != NUL) |
| 1588 | MB_PTR_ADV(p); |
| 1589 | ptr = vim_strsave(p); |
| 1590 | if (ptr == NULL) |
| 1591 | goto end; |
| 1592 | ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE); |
| 1593 | vim_free(ptr); |
| 1594 | |
| 1595 | oldp = ml_get_curline(); |
| 1596 | p = oldp + curwin->w_cursor.col; |
| 1597 | if (dir == FORWARD && *p != NUL) |
| 1598 | MB_PTR_ADV(p); |
| 1599 | ptr = vim_strnsave(oldp, p - oldp); |
| 1600 | if (ptr == NULL) |
| 1601 | goto end; |
| 1602 | ml_replace(curwin->w_cursor.lnum, ptr, FALSE); |
| 1603 | ++nr_lines; |
| 1604 | dir = FORWARD; |
| 1605 | } |
| 1606 | if (flags & PUT_LINE_FORWARD) |
| 1607 | { |
| 1608 | // Must be "p" for a Visual block, put lines below the block. |
| 1609 | curwin->w_cursor = curbuf->b_visual.vi_end; |
| 1610 | dir = FORWARD; |
| 1611 | } |
| 1612 | curbuf->b_op_start = curwin->w_cursor; // default for '[ mark |
| 1613 | curbuf->b_op_end = curwin->w_cursor; // default for '] mark |
| 1614 | } |
| 1615 | |
| 1616 | if (flags & PUT_LINE) // :put command or "p" in Visual line mode. |
| 1617 | y_type = MLINE; |
| 1618 | |
| 1619 | if (y_size == 0 || y_array == NULL) |
| 1620 | { |
| 1621 | semsg(_("E353: Nothing in register %s"), |
| 1622 | regname == 0 ? (char_u *)"\"" : transchar(regname)); |
| 1623 | goto end; |
| 1624 | } |
| 1625 | |
| 1626 | if (y_type == MBLOCK) |
| 1627 | { |
| 1628 | lnum = curwin->w_cursor.lnum + y_size + 1; |
| 1629 | if (lnum > curbuf->b_ml.ml_line_count) |
| 1630 | lnum = curbuf->b_ml.ml_line_count + 1; |
| 1631 | if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) |
| 1632 | goto end; |
| 1633 | } |
| 1634 | else if (y_type == MLINE) |
| 1635 | { |
| 1636 | lnum = curwin->w_cursor.lnum; |
| 1637 | #ifdef FEAT_FOLDING |
| 1638 | // Correct line number for closed fold. Don't move the cursor yet, |
| 1639 | // u_save() uses it. |
| 1640 | if (dir == BACKWARD) |
| 1641 | (void)hasFolding(lnum, &lnum, NULL); |
| 1642 | else |
| 1643 | (void)hasFolding(lnum, NULL, &lnum); |
| 1644 | #endif |
| 1645 | if (dir == FORWARD) |
| 1646 | ++lnum; |
| 1647 | // In an empty buffer the empty line is going to be replaced, include |
| 1648 | // it in the saved lines. |
| 1649 | if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL) |
| 1650 | goto end; |
| 1651 | #ifdef FEAT_FOLDING |
| 1652 | if (dir == FORWARD) |
| 1653 | curwin->w_cursor.lnum = lnum - 1; |
| 1654 | else |
| 1655 | curwin->w_cursor.lnum = lnum; |
| 1656 | curbuf->b_op_start = curwin->w_cursor; // for mark_adjust() |
| 1657 | #endif |
| 1658 | } |
| 1659 | else if (u_save_cursor() == FAIL) |
| 1660 | goto end; |
| 1661 | |
| 1662 | yanklen = (int)STRLEN(y_array[0]); |
| 1663 | |
| 1664 | if (ve_flags == VE_ALL && y_type == MCHAR) |
| 1665 | { |
| 1666 | if (gchar_cursor() == TAB) |
| 1667 | { |
| 1668 | // Don't need to insert spaces when "p" on the last position of a |
| 1669 | // tab or "P" on the first position. |
| 1670 | #ifdef FEAT_VARTABS |
| 1671 | int viscol = getviscol(); |
| 1672 | if (dir == FORWARD |
| 1673 | ? tabstop_padding(viscol, curbuf->b_p_ts, |
| 1674 | curbuf->b_p_vts_array) != 1 |
| 1675 | : curwin->w_cursor.coladd > 0) |
| 1676 | coladvance_force(viscol); |
| 1677 | #else |
| 1678 | if (dir == FORWARD |
| 1679 | ? (int)curwin->w_cursor.coladd < curbuf->b_p_ts - 1 |
| 1680 | : curwin->w_cursor.coladd > 0) |
| 1681 | coladvance_force(getviscol()); |
| 1682 | #endif |
| 1683 | else |
| 1684 | curwin->w_cursor.coladd = 0; |
| 1685 | } |
| 1686 | else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL) |
| 1687 | coladvance_force(getviscol() + (dir == FORWARD)); |
| 1688 | } |
| 1689 | |
| 1690 | lnum = curwin->w_cursor.lnum; |
| 1691 | col = curwin->w_cursor.col; |
| 1692 | |
| 1693 | // Block mode |
| 1694 | if (y_type == MBLOCK) |
| 1695 | { |
| 1696 | int c = gchar_cursor(); |
| 1697 | colnr_T endcol2 = 0; |
| 1698 | |
| 1699 | if (dir == FORWARD && c != NUL) |
| 1700 | { |
| 1701 | if (ve_flags == VE_ALL) |
| 1702 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 1703 | else |
| 1704 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); |
| 1705 | |
| 1706 | if (has_mbyte) |
| 1707 | // move to start of next multi-byte character |
| 1708 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
| 1709 | else |
| 1710 | if (c != TAB || ve_flags != VE_ALL) |
| 1711 | ++curwin->w_cursor.col; |
| 1712 | ++col; |
| 1713 | } |
| 1714 | else |
| 1715 | getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); |
| 1716 | |
| 1717 | col += curwin->w_cursor.coladd; |
| 1718 | if (ve_flags == VE_ALL |
| 1719 | && (curwin->w_cursor.coladd > 0 |
| 1720 | || endcol2 == curwin->w_cursor.col)) |
| 1721 | { |
| 1722 | if (dir == FORWARD && c == NUL) |
| 1723 | ++col; |
| 1724 | if (dir != FORWARD && c != NUL) |
| 1725 | ++curwin->w_cursor.col; |
| 1726 | if (c == TAB) |
| 1727 | { |
| 1728 | if (dir == BACKWARD && curwin->w_cursor.col) |
| 1729 | curwin->w_cursor.col--; |
| 1730 | if (dir == FORWARD && col - 1 == endcol2) |
| 1731 | curwin->w_cursor.col++; |
| 1732 | } |
| 1733 | } |
| 1734 | curwin->w_cursor.coladd = 0; |
| 1735 | bd.textcol = 0; |
| 1736 | for (i = 0; i < y_size; ++i) |
| 1737 | { |
| 1738 | int spaces; |
| 1739 | char shortline; |
| 1740 | |
| 1741 | bd.startspaces = 0; |
| 1742 | bd.endspaces = 0; |
| 1743 | vcol = 0; |
| 1744 | delcount = 0; |
| 1745 | |
| 1746 | // add a new line |
| 1747 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 1748 | { |
| 1749 | if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"", |
| 1750 | (colnr_T)1, FALSE) == FAIL) |
| 1751 | break; |
| 1752 | ++nr_lines; |
| 1753 | } |
| 1754 | // get the old line and advance to the position to insert at |
| 1755 | oldp = ml_get_curline(); |
| 1756 | oldlen = (int)STRLEN(oldp); |
| 1757 | for (ptr = oldp; vcol < col && *ptr; ) |
| 1758 | { |
| 1759 | // Count a tab for what it's worth (if list mode not on) |
| 1760 | incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol); |
| 1761 | vcol += incr; |
| 1762 | } |
| 1763 | bd.textcol = (colnr_T)(ptr - oldp); |
| 1764 | |
| 1765 | shortline = (vcol < col) || (vcol == col && !*ptr) ; |
| 1766 | |
| 1767 | if (vcol < col) // line too short, padd with spaces |
| 1768 | bd.startspaces = col - vcol; |
| 1769 | else if (vcol > col) |
| 1770 | { |
| 1771 | bd.endspaces = vcol - col; |
| 1772 | bd.startspaces = incr - bd.endspaces; |
| 1773 | --bd.textcol; |
| 1774 | delcount = 1; |
| 1775 | if (has_mbyte) |
| 1776 | bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol); |
| 1777 | if (oldp[bd.textcol] != TAB) |
| 1778 | { |
| 1779 | // Only a Tab can be split into spaces. Other |
| 1780 | // characters will have to be moved to after the |
| 1781 | // block, causing misalignment. |
| 1782 | delcount = 0; |
| 1783 | bd.endspaces = 0; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | yanklen = (int)STRLEN(y_array[i]); |
| 1788 | |
| 1789 | // calculate number of spaces required to fill right side of block |
| 1790 | spaces = y_width + 1; |
| 1791 | for (j = 0; j < yanklen; j++) |
| 1792 | spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0); |
| 1793 | if (spaces < 0) |
| 1794 | spaces = 0; |
| 1795 | |
| 1796 | // insert the new text |
| 1797 | totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces; |
| 1798 | newp = alloc(totlen + oldlen + 1); |
| 1799 | if (newp == NULL) |
| 1800 | break; |
| 1801 | // copy part up to cursor to new line |
| 1802 | ptr = newp; |
| 1803 | mch_memmove(ptr, oldp, (size_t)bd.textcol); |
| 1804 | ptr += bd.textcol; |
| 1805 | // may insert some spaces before the new text |
| 1806 | vim_memset(ptr, ' ', (size_t)bd.startspaces); |
| 1807 | ptr += bd.startspaces; |
| 1808 | // insert the new text |
| 1809 | for (j = 0; j < count; ++j) |
| 1810 | { |
| 1811 | mch_memmove(ptr, y_array[i], (size_t)yanklen); |
| 1812 | ptr += yanklen; |
| 1813 | |
| 1814 | // insert block's trailing spaces only if there's text behind |
| 1815 | if ((j < count - 1 || !shortline) && spaces) |
| 1816 | { |
| 1817 | vim_memset(ptr, ' ', (size_t)spaces); |
| 1818 | ptr += spaces; |
| 1819 | } |
| 1820 | } |
| 1821 | // may insert some spaces after the new text |
| 1822 | vim_memset(ptr, ' ', (size_t)bd.endspaces); |
| 1823 | ptr += bd.endspaces; |
| 1824 | // move the text after the cursor to the end of the line. |
| 1825 | mch_memmove(ptr, oldp + bd.textcol + delcount, |
| 1826 | (size_t)(oldlen - bd.textcol - delcount + 1)); |
| 1827 | ml_replace(curwin->w_cursor.lnum, newp, FALSE); |
| 1828 | |
| 1829 | ++curwin->w_cursor.lnum; |
| 1830 | if (i == 0) |
| 1831 | curwin->w_cursor.col += bd.startspaces; |
| 1832 | } |
| 1833 | |
| 1834 | changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines); |
| 1835 | |
| 1836 | // Set '[ mark. |
| 1837 | curbuf->b_op_start = curwin->w_cursor; |
| 1838 | curbuf->b_op_start.lnum = lnum; |
| 1839 | |
| 1840 | // adjust '] mark |
| 1841 | curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; |
| 1842 | curbuf->b_op_end.col = bd.textcol + totlen - 1; |
| 1843 | curbuf->b_op_end.coladd = 0; |
| 1844 | if (flags & PUT_CURSEND) |
| 1845 | { |
| 1846 | colnr_T len; |
| 1847 | |
| 1848 | curwin->w_cursor = curbuf->b_op_end; |
| 1849 | curwin->w_cursor.col++; |
| 1850 | |
| 1851 | // in Insert mode we might be after the NUL, correct for that |
| 1852 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 1853 | if (curwin->w_cursor.col > len) |
| 1854 | curwin->w_cursor.col = len; |
| 1855 | } |
| 1856 | else |
| 1857 | curwin->w_cursor.lnum = lnum; |
| 1858 | } |
| 1859 | else |
| 1860 | { |
| 1861 | // Character or Line mode |
| 1862 | if (y_type == MCHAR) |
| 1863 | { |
| 1864 | // if type is MCHAR, FORWARD is the same as BACKWARD on the next |
| 1865 | // char |
| 1866 | if (dir == FORWARD && gchar_cursor() != NUL) |
| 1867 | { |
| 1868 | if (has_mbyte) |
| 1869 | { |
| 1870 | int bytelen = (*mb_ptr2len)(ml_get_cursor()); |
| 1871 | |
| 1872 | // put it on the next of the multi-byte character. |
| 1873 | col += bytelen; |
| 1874 | if (yanklen) |
| 1875 | { |
| 1876 | curwin->w_cursor.col += bytelen; |
| 1877 | curbuf->b_op_end.col += bytelen; |
| 1878 | } |
| 1879 | } |
| 1880 | else |
| 1881 | { |
| 1882 | ++col; |
| 1883 | if (yanklen) |
| 1884 | { |
| 1885 | ++curwin->w_cursor.col; |
| 1886 | ++curbuf->b_op_end.col; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | curbuf->b_op_start = curwin->w_cursor; |
| 1891 | } |
| 1892 | // Line mode: BACKWARD is the same as FORWARD on the previous line |
| 1893 | else if (dir == BACKWARD) |
| 1894 | --lnum; |
| 1895 | new_cursor = curwin->w_cursor; |
| 1896 | |
| 1897 | // simple case: insert into current line |
| 1898 | if (y_type == MCHAR && y_size == 1) |
| 1899 | { |
| 1900 | linenr_T end_lnum = 0; // init for gcc |
| 1901 | |
| 1902 | if (VIsual_active) |
| 1903 | { |
| 1904 | end_lnum = curbuf->b_visual.vi_end.lnum; |
| 1905 | if (end_lnum < curbuf->b_visual.vi_start.lnum) |
| 1906 | end_lnum = curbuf->b_visual.vi_start.lnum; |
| 1907 | } |
| 1908 | |
| 1909 | do { |
| 1910 | totlen = count * yanklen; |
| 1911 | if (totlen > 0) |
| 1912 | { |
| 1913 | oldp = ml_get(lnum); |
| 1914 | if (VIsual_active && col > (int)STRLEN(oldp)) |
| 1915 | { |
| 1916 | lnum++; |
| 1917 | continue; |
| 1918 | } |
| 1919 | newp = alloc(STRLEN(oldp) + totlen + 1); |
| 1920 | if (newp == NULL) |
| 1921 | goto end; // alloc() gave an error message |
| 1922 | mch_memmove(newp, oldp, (size_t)col); |
| 1923 | ptr = newp + col; |
| 1924 | for (i = 0; i < count; ++i) |
| 1925 | { |
| 1926 | mch_memmove(ptr, y_array[0], (size_t)yanklen); |
| 1927 | ptr += yanklen; |
| 1928 | } |
| 1929 | STRMOVE(ptr, oldp + col); |
| 1930 | ml_replace(lnum, newp, FALSE); |
| 1931 | // Place cursor on last putted char. |
| 1932 | if (lnum == curwin->w_cursor.lnum) |
| 1933 | { |
| 1934 | // make sure curwin->w_virtcol is updated |
| 1935 | changed_cline_bef_curs(); |
| 1936 | curwin->w_cursor.col += (colnr_T)(totlen - 1); |
| 1937 | } |
| 1938 | } |
| 1939 | if (VIsual_active) |
| 1940 | lnum++; |
| 1941 | } while (VIsual_active && lnum <= end_lnum); |
| 1942 | |
| 1943 | if (VIsual_active) // reset lnum to the last visual line |
| 1944 | lnum--; |
| 1945 | |
| 1946 | curbuf->b_op_end = curwin->w_cursor; |
| 1947 | // For "CTRL-O p" in Insert mode, put cursor after last char |
| 1948 | if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND))) |
| 1949 | ++curwin->w_cursor.col; |
| 1950 | changed_bytes(lnum, col); |
| 1951 | } |
| 1952 | else |
| 1953 | { |
| 1954 | // Insert at least one line. When y_type is MCHAR, break the first |
| 1955 | // line in two. |
| 1956 | for (cnt = 1; cnt <= count; ++cnt) |
| 1957 | { |
| 1958 | i = 0; |
| 1959 | if (y_type == MCHAR) |
| 1960 | { |
| 1961 | // Split the current line in two at the insert position. |
| 1962 | // First insert y_array[size - 1] in front of second line. |
| 1963 | // Then append y_array[0] to first line. |
| 1964 | lnum = new_cursor.lnum; |
| 1965 | ptr = ml_get(lnum) + col; |
| 1966 | totlen = (int)STRLEN(y_array[y_size - 1]); |
| 1967 | newp = alloc(STRLEN(ptr) + totlen + 1); |
| 1968 | if (newp == NULL) |
| 1969 | goto error; |
| 1970 | STRCPY(newp, y_array[y_size - 1]); |
| 1971 | STRCAT(newp, ptr); |
| 1972 | // insert second line |
| 1973 | ml_append(lnum, newp, (colnr_T)0, FALSE); |
| 1974 | vim_free(newp); |
| 1975 | |
| 1976 | oldp = ml_get(lnum); |
| 1977 | newp = alloc(col + yanklen + 1); |
| 1978 | if (newp == NULL) |
| 1979 | goto error; |
| 1980 | // copy first part of line |
| 1981 | mch_memmove(newp, oldp, (size_t)col); |
| 1982 | // append to first line |
| 1983 | mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1)); |
| 1984 | ml_replace(lnum, newp, FALSE); |
| 1985 | |
| 1986 | curwin->w_cursor.lnum = lnum; |
| 1987 | i = 1; |
| 1988 | } |
| 1989 | |
| 1990 | for (; i < y_size; ++i) |
| 1991 | { |
| 1992 | if ((y_type != MCHAR || i < y_size - 1) |
| 1993 | && ml_append(lnum, y_array[i], (colnr_T)0, FALSE) |
| 1994 | == FAIL) |
| 1995 | goto error; |
| 1996 | lnum++; |
| 1997 | ++nr_lines; |
| 1998 | if (flags & PUT_FIXINDENT) |
| 1999 | { |
| 2000 | old_pos = curwin->w_cursor; |
| 2001 | curwin->w_cursor.lnum = lnum; |
| 2002 | ptr = ml_get(lnum); |
| 2003 | if (cnt == count && i == y_size - 1) |
| 2004 | lendiff = (int)STRLEN(ptr); |
| 2005 | #if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT) |
| 2006 | if (*ptr == '#' && preprocs_left()) |
| 2007 | indent = 0; // Leave # lines at start |
| 2008 | else |
| 2009 | #endif |
| 2010 | if (*ptr == NUL) |
| 2011 | indent = 0; // Ignore empty lines |
| 2012 | else if (first_indent) |
| 2013 | { |
| 2014 | indent_diff = orig_indent - get_indent(); |
| 2015 | indent = orig_indent; |
| 2016 | first_indent = FALSE; |
| 2017 | } |
| 2018 | else if ((indent = get_indent() + indent_diff) < 0) |
| 2019 | indent = 0; |
| 2020 | (void)set_indent(indent, 0); |
| 2021 | curwin->w_cursor = old_pos; |
| 2022 | // remember how many chars were removed |
| 2023 | if (cnt == count && i == y_size - 1) |
| 2024 | lendiff -= (int)STRLEN(ml_get(lnum)); |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | error: |
| 2030 | // Adjust marks. |
| 2031 | if (y_type == MLINE) |
| 2032 | { |
| 2033 | curbuf->b_op_start.col = 0; |
| 2034 | if (dir == FORWARD) |
| 2035 | curbuf->b_op_start.lnum++; |
| 2036 | } |
| 2037 | // Skip mark_adjust when adding lines after the last one, there |
| 2038 | // can't be marks there. But still needed in diff mode. |
| 2039 | if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines |
| 2040 | < curbuf->b_ml.ml_line_count |
| 2041 | #ifdef FEAT_DIFF |
| 2042 | || curwin->w_p_diff |
| 2043 | #endif |
| 2044 | ) |
| 2045 | mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR), |
| 2046 | (linenr_T)MAXLNUM, nr_lines, 0L); |
| 2047 | |
| 2048 | // note changed text for displaying and folding |
| 2049 | if (y_type == MCHAR) |
| 2050 | changed_lines(curwin->w_cursor.lnum, col, |
| 2051 | curwin->w_cursor.lnum + 1, nr_lines); |
| 2052 | else |
| 2053 | changed_lines(curbuf->b_op_start.lnum, 0, |
| 2054 | curbuf->b_op_start.lnum, nr_lines); |
| 2055 | |
| 2056 | // put '] mark at last inserted character |
| 2057 | curbuf->b_op_end.lnum = lnum; |
| 2058 | // correct length for change in indent |
| 2059 | col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; |
| 2060 | if (col > 1) |
| 2061 | curbuf->b_op_end.col = col - 1; |
| 2062 | else |
| 2063 | curbuf->b_op_end.col = 0; |
| 2064 | |
| 2065 | if (flags & PUT_CURSLINE) |
| 2066 | { |
| 2067 | // ":put": put cursor on last inserted line |
| 2068 | curwin->w_cursor.lnum = lnum; |
| 2069 | beginline(BL_WHITE | BL_FIX); |
| 2070 | } |
| 2071 | else if (flags & PUT_CURSEND) |
| 2072 | { |
| 2073 | // put cursor after inserted text |
| 2074 | if (y_type == MLINE) |
| 2075 | { |
| 2076 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 2077 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 2078 | else |
| 2079 | curwin->w_cursor.lnum = lnum + 1; |
| 2080 | curwin->w_cursor.col = 0; |
| 2081 | } |
| 2082 | else |
| 2083 | { |
| 2084 | curwin->w_cursor.lnum = lnum; |
| 2085 | curwin->w_cursor.col = col; |
| 2086 | } |
| 2087 | } |
| 2088 | else if (y_type == MLINE) |
| 2089 | { |
| 2090 | // put cursor on first non-blank in first inserted line |
| 2091 | curwin->w_cursor.col = 0; |
| 2092 | if (dir == FORWARD) |
| 2093 | ++curwin->w_cursor.lnum; |
| 2094 | beginline(BL_WHITE | BL_FIX); |
| 2095 | } |
| 2096 | else // put cursor on first inserted character |
| 2097 | curwin->w_cursor = new_cursor; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | msgmore(nr_lines); |
| 2102 | curwin->w_set_curswant = TRUE; |
| 2103 | |
| 2104 | end: |
| 2105 | if (allocated) |
| 2106 | vim_free(insert_string); |
| 2107 | if (regname == '=') |
| 2108 | vim_free(y_array); |
| 2109 | |
| 2110 | VIsual_active = FALSE; |
| 2111 | |
| 2112 | // If the cursor is past the end of the line put it at the end. |
| 2113 | adjust_cursor_eol(); |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Return the character name of the register with the given number. |
| 2118 | */ |
| 2119 | int |
| 2120 | get_register_name(int num) |
| 2121 | { |
| 2122 | if (num == -1) |
| 2123 | return '"'; |
| 2124 | else if (num < 10) |
| 2125 | return num + '0'; |
| 2126 | else if (num == DELETION_REGISTER) |
| 2127 | return '-'; |
| 2128 | #ifdef FEAT_CLIPBOARD |
| 2129 | else if (num == STAR_REGISTER) |
| 2130 | return '*'; |
| 2131 | else if (num == PLUS_REGISTER) |
| 2132 | return '+'; |
| 2133 | #endif |
| 2134 | else |
| 2135 | { |
| 2136 | #ifdef EBCDIC |
| 2137 | int i; |
| 2138 | |
| 2139 | // EBCDIC is really braindead ... |
| 2140 | i = 'a' + (num - 10); |
| 2141 | if (i > 'i') |
| 2142 | i += 7; |
| 2143 | if (i > 'r') |
| 2144 | i += 8; |
| 2145 | return i; |
| 2146 | #else |
| 2147 | return num + 'a' - 10; |
| 2148 | #endif |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * ":dis" and ":registers": Display the contents of the yank registers. |
| 2154 | */ |
| 2155 | void |
| 2156 | ex_display(exarg_T *eap) |
| 2157 | { |
| 2158 | int i, n; |
| 2159 | long j; |
| 2160 | char_u *p; |
| 2161 | yankreg_T *yb; |
| 2162 | int name; |
| 2163 | int attr; |
| 2164 | char_u *arg = eap->arg; |
| 2165 | int clen; |
| 2166 | |
| 2167 | if (arg != NULL && *arg == NUL) |
| 2168 | arg = NULL; |
| 2169 | attr = HL_ATTR(HLF_8); |
| 2170 | |
| 2171 | // Highlight title |
| 2172 | msg_puts_title(_("\n--- Registers ---")); |
| 2173 | for (i = -1; i < NUM_REGISTERS && !got_int; ++i) |
| 2174 | { |
| 2175 | name = get_register_name(i); |
| 2176 | if (arg != NULL && vim_strchr(arg, name) == NULL |
| 2177 | #ifdef ONE_CLIPBOARD |
| 2178 | // Star register and plus register contain the same thing. |
| 2179 | && (name != '*' || vim_strchr(arg, '+') == NULL) |
| 2180 | #endif |
| 2181 | ) |
| 2182 | continue; // did not ask for this register |
| 2183 | |
| 2184 | #ifdef FEAT_CLIPBOARD |
| 2185 | // Adjust register name for "unnamed" in 'clipboard'. |
| 2186 | // When it's a clipboard register, fill it with the current contents |
| 2187 | // of the clipboard. |
| 2188 | adjust_clip_reg(&name); |
| 2189 | (void)may_get_selection(name); |
| 2190 | #endif |
| 2191 | |
| 2192 | if (i == -1) |
| 2193 | { |
| 2194 | if (y_previous != NULL) |
| 2195 | yb = y_previous; |
| 2196 | else |
| 2197 | yb = &(y_regs[0]); |
| 2198 | } |
| 2199 | else |
| 2200 | yb = &(y_regs[i]); |
| 2201 | |
| 2202 | #ifdef FEAT_EVAL |
| 2203 | if (name == MB_TOLOWER(redir_reg) |
| 2204 | || (redir_reg == '"' && yb == y_previous)) |
| 2205 | continue; // do not list register being written to, the |
| 2206 | // pointer can be freed |
| 2207 | #endif |
| 2208 | |
| 2209 | if (yb->y_array != NULL) |
| 2210 | { |
| 2211 | msg_putchar('\n'); |
| 2212 | msg_putchar('"'); |
| 2213 | msg_putchar(name); |
| 2214 | msg_puts(" "); |
| 2215 | |
| 2216 | n = (int)Columns - 6; |
| 2217 | for (j = 0; j < yb->y_size && n > 1; ++j) |
| 2218 | { |
| 2219 | if (j) |
| 2220 | { |
| 2221 | msg_puts_attr("^J", attr); |
| 2222 | n -= 2; |
| 2223 | } |
| 2224 | for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; ++p) |
| 2225 | { |
| 2226 | clen = (*mb_ptr2len)(p); |
| 2227 | msg_outtrans_len(p, clen); |
| 2228 | p += clen - 1; |
| 2229 | } |
| 2230 | } |
| 2231 | if (n > 1 && yb->y_type == MLINE) |
| 2232 | msg_puts_attr("^J", attr); |
| 2233 | out_flush(); // show one line at a time |
| 2234 | } |
| 2235 | ui_breakcheck(); |
| 2236 | } |
| 2237 | |
| 2238 | // display last inserted text |
| 2239 | if ((p = get_last_insert()) != NULL |
| 2240 | && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) |
| 2241 | { |
| 2242 | msg_puts("\n\". "); |
| 2243 | dis_msg(p, TRUE); |
| 2244 | } |
| 2245 | |
| 2246 | // display last command line |
| 2247 | if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) |
| 2248 | && !got_int) |
| 2249 | { |
| 2250 | msg_puts("\n\": "); |
| 2251 | dis_msg(last_cmdline, FALSE); |
| 2252 | } |
| 2253 | |
| 2254 | // display current file name |
| 2255 | if (curbuf->b_fname != NULL |
| 2256 | && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 2257 | { |
| 2258 | msg_puts("\n\"% "); |
| 2259 | dis_msg(curbuf->b_fname, FALSE); |
| 2260 | } |
| 2261 | |
| 2262 | // display alternate file name |
| 2263 | if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) |
| 2264 | { |
| 2265 | char_u *fname; |
| 2266 | linenr_T dummy; |
| 2267 | |
| 2268 | if (buflist_name_nr(0, &fname, &dummy) != FAIL) |
| 2269 | { |
| 2270 | msg_puts("\n\"# "); |
| 2271 | dis_msg(fname, FALSE); |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | // display last search pattern |
| 2276 | if (last_search_pat() != NULL |
| 2277 | && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) |
| 2278 | { |
| 2279 | msg_puts("\n\"/ "); |
| 2280 | dis_msg(last_search_pat(), FALSE); |
| 2281 | } |
| 2282 | |
| 2283 | #ifdef FEAT_EVAL |
| 2284 | // display last used expression |
| 2285 | if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) |
| 2286 | && !got_int) |
| 2287 | { |
| 2288 | msg_puts("\n\"= "); |
| 2289 | dis_msg(expr_line, FALSE); |
| 2290 | } |
| 2291 | #endif |
| 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | * display a string for do_dis() |
| 2296 | * truncate at end of screen line |
| 2297 | */ |
| 2298 | static void |
| 2299 | dis_msg( |
| 2300 | char_u *p, |
| 2301 | int skip_esc) // if TRUE, ignore trailing ESC |
| 2302 | { |
| 2303 | int n; |
| 2304 | int l; |
| 2305 | |
| 2306 | n = (int)Columns - 6; |
| 2307 | while (*p != NUL |
| 2308 | && !(*p == ESC && skip_esc && *(p + 1) == NUL) |
| 2309 | && (n -= ptr2cells(p)) >= 0) |
| 2310 | { |
| 2311 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 2312 | { |
| 2313 | msg_outtrans_len(p, l); |
| 2314 | p += l; |
| 2315 | } |
| 2316 | else |
| 2317 | msg_outtrans_len(p++, 1); |
| 2318 | } |
| 2319 | ui_breakcheck(); |
| 2320 | } |
| 2321 | |
| 2322 | #if defined(FEAT_CLIPBOARD) || defined(PROTO) |
| 2323 | void |
| 2324 | clip_free_selection(Clipboard_T *cbd) |
| 2325 | { |
| 2326 | yankreg_T *y_ptr = y_current; |
| 2327 | |
| 2328 | if (cbd == &clip_plus) |
| 2329 | y_current = &y_regs[PLUS_REGISTER]; |
| 2330 | else |
| 2331 | y_current = &y_regs[STAR_REGISTER]; |
| 2332 | free_yank_all(); |
| 2333 | y_current->y_size = 0; |
| 2334 | y_current = y_ptr; |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * Get the selected text and put it in register '*' or '+'. |
| 2339 | */ |
| 2340 | void |
| 2341 | clip_get_selection(Clipboard_T *cbd) |
| 2342 | { |
| 2343 | yankreg_T *old_y_previous, *old_y_current; |
| 2344 | pos_T old_cursor; |
| 2345 | pos_T old_visual; |
| 2346 | int old_visual_mode; |
| 2347 | colnr_T old_curswant; |
| 2348 | int old_set_curswant; |
| 2349 | pos_T old_op_start, old_op_end; |
| 2350 | oparg_T oa; |
| 2351 | cmdarg_T ca; |
| 2352 | |
| 2353 | if (cbd->owned) |
| 2354 | { |
| 2355 | if ((cbd == &clip_plus && y_regs[PLUS_REGISTER].y_array != NULL) |
| 2356 | || (cbd == &clip_star && y_regs[STAR_REGISTER].y_array != NULL)) |
| 2357 | return; |
| 2358 | |
| 2359 | // Get the text between clip_star.start & clip_star.end |
| 2360 | old_y_previous = y_previous; |
| 2361 | old_y_current = y_current; |
| 2362 | old_cursor = curwin->w_cursor; |
| 2363 | old_curswant = curwin->w_curswant; |
| 2364 | old_set_curswant = curwin->w_set_curswant; |
| 2365 | old_op_start = curbuf->b_op_start; |
| 2366 | old_op_end = curbuf->b_op_end; |
| 2367 | old_visual = VIsual; |
| 2368 | old_visual_mode = VIsual_mode; |
| 2369 | clear_oparg(&oa); |
| 2370 | oa.regname = (cbd == &clip_plus ? '+' : '*'); |
| 2371 | oa.op_type = OP_YANK; |
| 2372 | vim_memset(&ca, 0, sizeof(ca)); |
| 2373 | ca.oap = &oa; |
| 2374 | ca.cmdchar = 'y'; |
| 2375 | ca.count1 = 1; |
| 2376 | ca.retval = CA_NO_ADJ_OP_END; |
| 2377 | do_pending_operator(&ca, 0, TRUE); |
| 2378 | y_previous = old_y_previous; |
| 2379 | y_current = old_y_current; |
| 2380 | curwin->w_cursor = old_cursor; |
| 2381 | changed_cline_bef_curs(); // need to update w_virtcol et al |
| 2382 | curwin->w_curswant = old_curswant; |
| 2383 | curwin->w_set_curswant = old_set_curswant; |
| 2384 | curbuf->b_op_start = old_op_start; |
| 2385 | curbuf->b_op_end = old_op_end; |
| 2386 | VIsual = old_visual; |
| 2387 | VIsual_mode = old_visual_mode; |
| 2388 | } |
| 2389 | else if (!is_clipboard_needs_update()) |
| 2390 | { |
| 2391 | clip_free_selection(cbd); |
| 2392 | |
| 2393 | // Try to get selected text from another window |
| 2394 | clip_gen_request_selection(cbd); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | /* |
| 2399 | * Convert from the GUI selection string into the '*'/'+' register. |
| 2400 | */ |
| 2401 | void |
| 2402 | clip_yank_selection( |
| 2403 | int type, |
| 2404 | char_u *str, |
| 2405 | long len, |
| 2406 | Clipboard_T *cbd) |
| 2407 | { |
| 2408 | yankreg_T *y_ptr; |
| 2409 | |
| 2410 | if (cbd == &clip_plus) |
| 2411 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 2412 | else |
| 2413 | y_ptr = &y_regs[STAR_REGISTER]; |
| 2414 | |
| 2415 | clip_free_selection(cbd); |
| 2416 | |
| 2417 | str_to_reg(y_ptr, type, str, len, 0L, FALSE); |
| 2418 | } |
| 2419 | |
| 2420 | /* |
| 2421 | * Convert the '*'/'+' register into a GUI selection string returned in *str |
| 2422 | * with length *len. |
| 2423 | * Returns the motion type, or -1 for failure. |
| 2424 | */ |
| 2425 | int |
| 2426 | clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd) |
| 2427 | { |
| 2428 | char_u *p; |
| 2429 | int lnum; |
| 2430 | int i, j; |
| 2431 | int_u eolsize; |
| 2432 | yankreg_T *y_ptr; |
| 2433 | |
| 2434 | if (cbd == &clip_plus) |
| 2435 | y_ptr = &y_regs[PLUS_REGISTER]; |
| 2436 | else |
| 2437 | y_ptr = &y_regs[STAR_REGISTER]; |
| 2438 | |
| 2439 | # ifdef USE_CRNL |
| 2440 | eolsize = 2; |
| 2441 | # else |
| 2442 | eolsize = 1; |
| 2443 | # endif |
| 2444 | |
| 2445 | *str = NULL; |
| 2446 | *len = 0; |
| 2447 | if (y_ptr->y_array == NULL) |
| 2448 | return -1; |
| 2449 | |
| 2450 | for (i = 0; i < y_ptr->y_size; i++) |
| 2451 | *len += (long_u)STRLEN(y_ptr->y_array[i]) + eolsize; |
| 2452 | |
| 2453 | // Don't want newline character at end of last line if we're in MCHAR mode. |
| 2454 | if (y_ptr->y_type == MCHAR && *len >= eolsize) |
| 2455 | *len -= eolsize; |
| 2456 | |
| 2457 | p = *str = alloc(*len + 1); // add one to avoid zero |
| 2458 | if (p == NULL) |
| 2459 | return -1; |
| 2460 | lnum = 0; |
| 2461 | for (i = 0, j = 0; i < (int)*len; i++, j++) |
| 2462 | { |
| 2463 | if (y_ptr->y_array[lnum][j] == '\n') |
| 2464 | p[i] = NUL; |
| 2465 | else if (y_ptr->y_array[lnum][j] == NUL) |
| 2466 | { |
| 2467 | # ifdef USE_CRNL |
| 2468 | p[i++] = '\r'; |
| 2469 | # endif |
| 2470 | p[i] = '\n'; |
| 2471 | lnum++; |
| 2472 | j = -1; |
| 2473 | } |
| 2474 | else |
| 2475 | p[i] = y_ptr->y_array[lnum][j]; |
| 2476 | } |
| 2477 | return y_ptr->y_type; |
| 2478 | } |
| 2479 | |
| 2480 | |
| 2481 | /* |
| 2482 | * If we have written to a clipboard register, send the text to the clipboard. |
| 2483 | */ |
| 2484 | static void |
| 2485 | may_set_selection(void) |
| 2486 | { |
| 2487 | if (y_current == &(y_regs[STAR_REGISTER]) && clip_star.available) |
| 2488 | { |
| 2489 | clip_own_selection(&clip_star); |
| 2490 | clip_gen_set_selection(&clip_star); |
| 2491 | } |
| 2492 | else if (y_current == &(y_regs[PLUS_REGISTER]) && clip_plus.available) |
| 2493 | { |
| 2494 | clip_own_selection(&clip_plus); |
| 2495 | clip_gen_set_selection(&clip_plus); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | #endif // FEAT_CLIPBOARD || PROTO |
| 2500 | |
| 2501 | |
| 2502 | #if defined(FEAT_DND) || defined(PROTO) |
| 2503 | /* |
| 2504 | * Replace the contents of the '~' register with str. |
| 2505 | */ |
| 2506 | void |
| 2507 | dnd_yank_drag_data(char_u *str, long len) |
| 2508 | { |
| 2509 | yankreg_T *curr; |
| 2510 | |
| 2511 | curr = y_current; |
| 2512 | y_current = &y_regs[TILDE_REGISTER]; |
| 2513 | free_yank_all(); |
| 2514 | str_to_reg(y_current, MCHAR, str, len, 0L, FALSE); |
| 2515 | y_current = curr; |
| 2516 | } |
| 2517 | #endif |
| 2518 | |
| 2519 | |
| 2520 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2521 | /* |
| 2522 | * Return the type of a register. |
| 2523 | * Used for getregtype() |
| 2524 | * Returns MAUTO for error. |
| 2525 | */ |
| 2526 | char_u |
| 2527 | get_reg_type(int regname, long *reglen) |
| 2528 | { |
| 2529 | switch (regname) |
| 2530 | { |
| 2531 | case '%': // file name |
| 2532 | case '#': // alternate file name |
| 2533 | case '=': // expression |
| 2534 | case ':': // last command line |
| 2535 | case '/': // last search-pattern |
| 2536 | case '.': // last inserted text |
| 2537 | # ifdef FEAT_SEARCHPATH |
| 2538 | case Ctrl_F: // Filename under cursor |
| 2539 | case Ctrl_P: // Path under cursor, expand via "path" |
| 2540 | # endif |
| 2541 | case Ctrl_W: // word under cursor |
| 2542 | case Ctrl_A: // WORD (mnemonic All) under cursor |
| 2543 | case '_': // black hole: always empty |
| 2544 | return MCHAR; |
| 2545 | } |
| 2546 | |
| 2547 | # ifdef FEAT_CLIPBOARD |
| 2548 | regname = may_get_selection(regname); |
| 2549 | # endif |
| 2550 | |
| 2551 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 2552 | return MAUTO; |
| 2553 | |
| 2554 | get_yank_register(regname, FALSE); |
| 2555 | |
| 2556 | if (y_current->y_array != NULL) |
| 2557 | { |
| 2558 | if (reglen != NULL && y_current->y_type == MBLOCK) |
| 2559 | *reglen = y_current->y_width; |
| 2560 | return y_current->y_type; |
| 2561 | } |
| 2562 | return MAUTO; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * When "flags" has GREG_LIST return a list with text "s". |
| 2567 | * Otherwise just return "s". |
| 2568 | */ |
| 2569 | static char_u * |
| 2570 | getreg_wrap_one_line(char_u *s, int flags) |
| 2571 | { |
| 2572 | if (flags & GREG_LIST) |
| 2573 | { |
| 2574 | list_T *list = list_alloc(); |
| 2575 | |
| 2576 | if (list != NULL) |
| 2577 | { |
| 2578 | if (list_append_string(list, NULL, -1) == FAIL) |
| 2579 | { |
| 2580 | list_free(list); |
| 2581 | return NULL; |
| 2582 | } |
| 2583 | list->lv_first->li_tv.vval.v_string = s; |
| 2584 | } |
| 2585 | return (char_u *)list; |
| 2586 | } |
| 2587 | return s; |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * Return the contents of a register as a single allocated string. |
| 2592 | * Used for "@r" in expressions and for getreg(). |
| 2593 | * Returns NULL for error. |
| 2594 | * Flags: |
| 2595 | * GREG_NO_EXPR Do not allow expression register |
| 2596 | * GREG_EXPR_SRC For the expression register: return expression itself, |
| 2597 | * not the result of its evaluation. |
| 2598 | * GREG_LIST Return a list of lines in place of a single string. |
| 2599 | */ |
| 2600 | char_u * |
| 2601 | get_reg_contents(int regname, int flags) |
| 2602 | { |
| 2603 | long i; |
| 2604 | char_u *retval; |
| 2605 | int allocated; |
| 2606 | long len; |
| 2607 | |
| 2608 | // Don't allow using an expression register inside an expression |
| 2609 | if (regname == '=') |
| 2610 | { |
| 2611 | if (flags & GREG_NO_EXPR) |
| 2612 | return NULL; |
| 2613 | if (flags & GREG_EXPR_SRC) |
| 2614 | return getreg_wrap_one_line(get_expr_line_src(), flags); |
| 2615 | return getreg_wrap_one_line(get_expr_line(), flags); |
| 2616 | } |
| 2617 | |
| 2618 | if (regname == '@') // "@@" is used for unnamed register |
| 2619 | regname = '"'; |
| 2620 | |
| 2621 | // check for valid regname |
| 2622 | if (regname != NUL && !valid_yank_reg(regname, FALSE)) |
| 2623 | return NULL; |
| 2624 | |
| 2625 | # ifdef FEAT_CLIPBOARD |
| 2626 | regname = may_get_selection(regname); |
| 2627 | # endif |
| 2628 | |
| 2629 | if (get_spec_reg(regname, &retval, &allocated, FALSE)) |
| 2630 | { |
| 2631 | if (retval == NULL) |
| 2632 | return NULL; |
| 2633 | if (allocated) |
| 2634 | return getreg_wrap_one_line(retval, flags); |
| 2635 | return getreg_wrap_one_line(vim_strsave(retval), flags); |
| 2636 | } |
| 2637 | |
| 2638 | get_yank_register(regname, FALSE); |
| 2639 | if (y_current->y_array == NULL) |
| 2640 | return NULL; |
| 2641 | |
| 2642 | if (flags & GREG_LIST) |
| 2643 | { |
| 2644 | list_T *list = list_alloc(); |
| 2645 | int error = FALSE; |
| 2646 | |
| 2647 | if (list == NULL) |
| 2648 | return NULL; |
| 2649 | for (i = 0; i < y_current->y_size; ++i) |
| 2650 | if (list_append_string(list, y_current->y_array[i], -1) == FAIL) |
| 2651 | error = TRUE; |
| 2652 | if (error) |
| 2653 | { |
| 2654 | list_free(list); |
| 2655 | return NULL; |
| 2656 | } |
| 2657 | return (char_u *)list; |
| 2658 | } |
| 2659 | |
| 2660 | // Compute length of resulting string. |
| 2661 | len = 0; |
| 2662 | for (i = 0; i < y_current->y_size; ++i) |
| 2663 | { |
| 2664 | len += (long)STRLEN(y_current->y_array[i]); |
| 2665 | // Insert a newline between lines and after last line if |
| 2666 | // y_type is MLINE. |
| 2667 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 2668 | ++len; |
| 2669 | } |
| 2670 | |
| 2671 | retval = alloc(len + 1); |
| 2672 | |
| 2673 | // Copy the lines of the yank register into the string. |
| 2674 | if (retval != NULL) |
| 2675 | { |
| 2676 | len = 0; |
| 2677 | for (i = 0; i < y_current->y_size; ++i) |
| 2678 | { |
| 2679 | STRCPY(retval + len, y_current->y_array[i]); |
| 2680 | len += (long)STRLEN(retval + len); |
| 2681 | |
| 2682 | // Insert a NL between lines and after the last line if y_type is |
| 2683 | // MLINE. |
| 2684 | if (y_current->y_type == MLINE || i < y_current->y_size - 1) |
| 2685 | retval[len++] = '\n'; |
| 2686 | } |
| 2687 | retval[len] = NUL; |
| 2688 | } |
| 2689 | |
| 2690 | return retval; |
| 2691 | } |
| 2692 | |
| 2693 | static int |
| 2694 | init_write_reg( |
| 2695 | int name, |
| 2696 | yankreg_T **old_y_previous, |
| 2697 | yankreg_T **old_y_current, |
| 2698 | int must_append, |
| 2699 | int *yank_type UNUSED) |
| 2700 | { |
| 2701 | if (!valid_yank_reg(name, TRUE)) // check for valid reg name |
| 2702 | { |
| 2703 | emsg_invreg(name); |
| 2704 | return FAIL; |
| 2705 | } |
| 2706 | |
| 2707 | // Don't want to change the current (unnamed) register |
| 2708 | *old_y_previous = y_previous; |
| 2709 | *old_y_current = y_current; |
| 2710 | |
| 2711 | get_yank_register(name, TRUE); |
| 2712 | if (!y_append && !must_append) |
| 2713 | free_yank_all(); |
| 2714 | return OK; |
| 2715 | } |
| 2716 | |
| 2717 | static void |
| 2718 | finish_write_reg( |
| 2719 | int name, |
| 2720 | yankreg_T *old_y_previous, |
| 2721 | yankreg_T *old_y_current) |
| 2722 | { |
| 2723 | # ifdef FEAT_CLIPBOARD |
| 2724 | // Send text of clipboard register to the clipboard. |
| 2725 | may_set_selection(); |
| 2726 | # endif |
| 2727 | |
| 2728 | // ':let @" = "val"' should change the meaning of the "" register |
| 2729 | if (name != '"') |
| 2730 | y_previous = old_y_previous; |
| 2731 | y_current = old_y_current; |
| 2732 | } |
| 2733 | |
| 2734 | /* |
| 2735 | * Store string "str" in register "name". |
| 2736 | * "maxlen" is the maximum number of bytes to use, -1 for all bytes. |
| 2737 | * If "must_append" is TRUE, always append to the register. Otherwise append |
| 2738 | * if "name" is an uppercase letter. |
| 2739 | * Note: "maxlen" and "must_append" don't work for the "/" register. |
| 2740 | * Careful: 'str' is modified, you may have to use a copy! |
| 2741 | * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise. |
| 2742 | */ |
| 2743 | void |
| 2744 | write_reg_contents( |
| 2745 | int name, |
| 2746 | char_u *str, |
| 2747 | int maxlen, |
| 2748 | int must_append) |
| 2749 | { |
| 2750 | write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L); |
| 2751 | } |
| 2752 | |
| 2753 | void |
| 2754 | write_reg_contents_lst( |
| 2755 | int name, |
| 2756 | char_u **strings, |
| 2757 | int maxlen UNUSED, |
| 2758 | int must_append, |
| 2759 | int yank_type, |
| 2760 | long block_len) |
| 2761 | { |
| 2762 | yankreg_T *old_y_previous, *old_y_current; |
| 2763 | |
| 2764 | if (name == '/' || name == '=') |
| 2765 | { |
| 2766 | char_u *s; |
| 2767 | |
| 2768 | if (strings[0] == NULL) |
| 2769 | s = (char_u *)""; |
| 2770 | else if (strings[1] != NULL) |
| 2771 | { |
| 2772 | emsg(_("E883: search pattern and expression register may not " |
| 2773 | "contain two or more lines")); |
| 2774 | return; |
| 2775 | } |
| 2776 | else |
| 2777 | s = strings[0]; |
| 2778 | write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len); |
| 2779 | return; |
| 2780 | } |
| 2781 | |
| 2782 | if (name == '_') // black hole: nothing to do |
| 2783 | return; |
| 2784 | |
| 2785 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 2786 | &yank_type) == FAIL) |
| 2787 | return; |
| 2788 | |
| 2789 | str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE); |
| 2790 | |
| 2791 | finish_write_reg(name, old_y_previous, old_y_current); |
| 2792 | } |
| 2793 | |
| 2794 | void |
| 2795 | write_reg_contents_ex( |
| 2796 | int name, |
| 2797 | char_u *str, |
| 2798 | int maxlen, |
| 2799 | int must_append, |
| 2800 | int yank_type, |
| 2801 | long block_len) |
| 2802 | { |
| 2803 | yankreg_T *old_y_previous, *old_y_current; |
| 2804 | long len; |
| 2805 | |
| 2806 | if (maxlen >= 0) |
| 2807 | len = maxlen; |
| 2808 | else |
| 2809 | len = (long)STRLEN(str); |
| 2810 | |
| 2811 | // Special case: '/' search pattern |
| 2812 | if (name == '/') |
| 2813 | { |
| 2814 | set_last_search_pat(str, RE_SEARCH, TRUE, TRUE); |
| 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | if (name == '#') |
| 2819 | { |
| 2820 | buf_T *buf; |
| 2821 | |
| 2822 | if (VIM_ISDIGIT(*str)) |
| 2823 | { |
| 2824 | int num = atoi((char *)str); |
| 2825 | |
| 2826 | buf = buflist_findnr(num); |
| 2827 | if (buf == NULL) |
| 2828 | semsg(_(e_nobufnr), (long)num); |
| 2829 | } |
| 2830 | else |
| 2831 | buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str), |
| 2832 | TRUE, FALSE, FALSE)); |
| 2833 | if (buf == NULL) |
| 2834 | return; |
| 2835 | curwin->w_alt_fnum = buf->b_fnum; |
| 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | if (name == '=') |
| 2840 | { |
| 2841 | char_u *p, *s; |
| 2842 | |
| 2843 | p = vim_strnsave(str, (int)len); |
| 2844 | if (p == NULL) |
| 2845 | return; |
| 2846 | if (must_append) |
| 2847 | { |
| 2848 | s = concat_str(get_expr_line_src(), p); |
| 2849 | vim_free(p); |
| 2850 | p = s; |
| 2851 | } |
| 2852 | set_expr_line(p); |
| 2853 | return; |
| 2854 | } |
| 2855 | |
| 2856 | if (name == '_') // black hole: nothing to do |
| 2857 | return; |
| 2858 | |
| 2859 | if (init_write_reg(name, &old_y_previous, &old_y_current, must_append, |
| 2860 | &yank_type) == FAIL) |
| 2861 | return; |
| 2862 | |
| 2863 | str_to_reg(y_current, yank_type, str, len, block_len, FALSE); |
| 2864 | |
| 2865 | finish_write_reg(name, old_y_previous, old_y_current); |
| 2866 | } |
| 2867 | #endif // FEAT_EVAL |
| 2868 | |
| 2869 | #if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL) |
| 2870 | /* |
| 2871 | * Put a string into a register. When the register is not empty, the string |
| 2872 | * is appended. |
| 2873 | */ |
| 2874 | static void |
| 2875 | str_to_reg( |
| 2876 | yankreg_T *y_ptr, // pointer to yank register |
| 2877 | int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO |
| 2878 | char_u *str, // string to put in register |
| 2879 | long len, // length of string |
| 2880 | long blocklen, // width of Visual block |
| 2881 | int str_list) // TRUE if str is char_u ** |
| 2882 | { |
| 2883 | int type; // MCHAR, MLINE or MBLOCK |
| 2884 | int lnum; |
| 2885 | long start; |
| 2886 | long i; |
| 2887 | int extra; |
| 2888 | int newlines; // number of lines added |
| 2889 | int extraline = 0; // extra line at the end |
| 2890 | int append = FALSE; // append to last line in register |
| 2891 | char_u *s; |
| 2892 | char_u **ss; |
| 2893 | char_u **pp; |
| 2894 | long maxlen; |
| 2895 | |
| 2896 | if (y_ptr->y_array == NULL) // NULL means empty register |
| 2897 | y_ptr->y_size = 0; |
| 2898 | |
| 2899 | if (yank_type == MAUTO) |
| 2900 | type = ((str_list || (len > 0 && (str[len - 1] == NL |
| 2901 | || str[len - 1] == CAR))) |
| 2902 | ? MLINE : MCHAR); |
| 2903 | else |
| 2904 | type = yank_type; |
| 2905 | |
| 2906 | // Count the number of lines within the string |
| 2907 | newlines = 0; |
| 2908 | if (str_list) |
| 2909 | { |
| 2910 | for (ss = (char_u **) str; *ss != NULL; ++ss) |
| 2911 | ++newlines; |
| 2912 | } |
| 2913 | else |
| 2914 | { |
| 2915 | for (i = 0; i < len; i++) |
| 2916 | if (str[i] == '\n') |
| 2917 | ++newlines; |
| 2918 | if (type == MCHAR || len == 0 || str[len - 1] != '\n') |
| 2919 | { |
| 2920 | extraline = 1; |
| 2921 | ++newlines; // count extra newline at the end |
| 2922 | } |
| 2923 | if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR) |
| 2924 | { |
| 2925 | append = TRUE; |
| 2926 | --newlines; // uncount newline when appending first line |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | // Without any lines make the register empty. |
| 2931 | if (y_ptr->y_size + newlines == 0) |
| 2932 | { |
| 2933 | VIM_CLEAR(y_ptr->y_array); |
| 2934 | return; |
| 2935 | } |
| 2936 | |
| 2937 | // Allocate an array to hold the pointers to the new register lines. |
| 2938 | // If the register was not empty, move the existing lines to the new array. |
| 2939 | pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE); |
| 2940 | if (pp == NULL) // out of memory |
| 2941 | return; |
| 2942 | for (lnum = 0; lnum < y_ptr->y_size; ++lnum) |
| 2943 | pp[lnum] = y_ptr->y_array[lnum]; |
| 2944 | vim_free(y_ptr->y_array); |
| 2945 | y_ptr->y_array = pp; |
| 2946 | maxlen = 0; |
| 2947 | |
| 2948 | // Find the end of each line and save it into the array. |
| 2949 | if (str_list) |
| 2950 | { |
| 2951 | for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum) |
| 2952 | { |
| 2953 | i = (long)STRLEN(*ss); |
| 2954 | pp[lnum] = vim_strnsave(*ss, i); |
| 2955 | if (i > maxlen) |
| 2956 | maxlen = i; |
| 2957 | } |
| 2958 | } |
| 2959 | else |
| 2960 | { |
| 2961 | for (start = 0; start < len + extraline; start += i + 1) |
| 2962 | { |
| 2963 | for (i = start; i < len; ++i) // find the end of the line |
| 2964 | if (str[i] == '\n') |
| 2965 | break; |
| 2966 | i -= start; // i is now length of line |
| 2967 | if (i > maxlen) |
| 2968 | maxlen = i; |
| 2969 | if (append) |
| 2970 | { |
| 2971 | --lnum; |
| 2972 | extra = (int)STRLEN(y_ptr->y_array[lnum]); |
| 2973 | } |
| 2974 | else |
| 2975 | extra = 0; |
| 2976 | s = alloc(i + extra + 1); |
| 2977 | if (s == NULL) |
| 2978 | break; |
| 2979 | if (extra) |
| 2980 | mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra); |
| 2981 | if (append) |
| 2982 | vim_free(y_ptr->y_array[lnum]); |
| 2983 | if (i) |
| 2984 | mch_memmove(s + extra, str + start, (size_t)i); |
| 2985 | extra += i; |
| 2986 | s[extra] = NUL; |
| 2987 | y_ptr->y_array[lnum++] = s; |
| 2988 | while (--extra >= 0) |
| 2989 | { |
| 2990 | if (*s == NUL) |
| 2991 | *s = '\n'; // replace NUL with newline |
| 2992 | ++s; |
| 2993 | } |
| 2994 | append = FALSE; // only first line is appended |
| 2995 | } |
| 2996 | } |
| 2997 | y_ptr->y_type = type; |
| 2998 | y_ptr->y_size = lnum; |
| 2999 | if (type == MBLOCK) |
| 3000 | y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen); |
| 3001 | else |
| 3002 | y_ptr->y_width = 0; |
| 3003 | # ifdef FEAT_VIMINFO |
| 3004 | y_ptr->y_time_set = vim_time(); |
| 3005 | # endif |
| 3006 | } |
| 3007 | #endif // FEAT_CLIPBOARD || FEAT_EVAL || PROTO |