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