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