blob: 5dc8f2896a4894a3fe9caefbe15e2d284240a8b4 [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);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +020035static int yank_copy_line(struct block_def *bd, long y_idx);
36#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
77#if defined(FEAT_EVAL) || defined(PROTO)
78/*
79 * Keep the last expression line here, for repeating.
80 */
81static char_u *expr_line = NULL;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +010082static exarg_T *expr_eap = NULL;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +020083
84/*
85 * Get an expression for the "\"=expr1" or "CTRL-R =expr1"
86 * Returns '=' when OK, NUL otherwise.
87 */
88 int
89get_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 Moolenaarb4bcea42020-10-28 13:53:50 +010099 set_expr_line(new_line, NULL);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200100 return '=';
101}
102
103/*
104 * Set the expression for the '=' register.
105 * Argument must be an allocated string.
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100106 * "eap" may be used if the next line needs to be checked when evaluating the
107 * expression.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200108 */
109 void
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100110set_expr_line(char_u *new_line, exarg_T *eap)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200111{
112 vim_free(expr_line);
113 expr_line = new_line;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100114 expr_eap = eap;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200115}
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 *
122get_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 Moolenaarb4bcea42020-10-28 13:53:50 +0100143 rv = eval_to_string_eap(expr_copy, TRUE, expr_eap);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200144 --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 *
153get_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
166valid_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
206get_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 Moolenaar4aea03e2019-09-25 22:37:17 +0200255/*
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 *
260get_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
312put_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 Moolenaarfccbf062020-11-26 20:34:00 +0100325#if defined(FEAT_CLIPBOARD) || defined(PROTO)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200326 void
327free_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 Moolenaar4aea03e2019-09-25 22:37:17 +0200339/*
340 * return TRUE if the current yank register has type MLINE
341 */
342 int
343yank_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 Moolenaar4aea03e2019-09-25 22:37:17 +0200352
353/*
354 * Start or stop recording into a yank register.
355 *
356 * Return FAIL for failure, OK otherwise.
357 */
358 int
359do_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
415stuff_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
465static int execreg_lastc = NUL;
466
467 int
468get_execreg_lastc(void)
469{
470 return execreg_lastc;
471}
472
473 void
474set_execreg_lastc(int lastc)
475{
476 execreg_lastc = lastc;
477}
478
479/*
Bram Moolenaar856c1112020-06-17 21:47:23 +0200480 * 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 *
496execreg_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 Moolenaar4aea03e2019-09-25 22:37:17 +0200547 * Execute a yank register: copy it into the stuff buffer.
548 *
549 * Return FAIL for failure, OK otherwise.
550 */
551 int
552do_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 Moolenaar4b96df52020-01-26 22:00:26 +0100644 // Disallow remapping for ":@r".
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200645 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 Moolenaar856c1112020-06-17 21:47:23 +0200652 char_u *str;
653 int free_str = FALSE;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200654
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 Moolenaar856c1112020-06-17 21:47:23 +0200662
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 Moolenaar4aea03e2019-09-25 22:37:17 +0200679 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
699put_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
728put_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
767insert_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 Moolenaar032a2d02020-12-22 17:59:35 +0100812 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 Moolenaar4aea03e2019-09-25 22:37:17 +0200820 // 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
836get_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
931cmdline_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 Moolenaar4aea03e2019-09-25 22:37:17 +0200962/*
963 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
964 */
965 void
966shift_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
982yank_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 Moolenaar6d90c612020-06-29 20:23:32 +02001002 (void)dict_add_list(v_event, "regcontents", list);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001003
1004 buf[0] = (char_u)oap->regname;
1005 buf[1] = NUL;
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001006 (void)dict_add_string(v_event, "regname", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001007
1008 buf[0] = get_op_char(oap->op_type);
1009 buf[1] = get_extra_op_char(oap->op_type);
1010 buf[2] = NUL;
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001011 (void)dict_add_string(v_event, "operator", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001012
1013 buf[0] = NUL;
1014 buf[1] = NUL;
1015 switch (get_reg_type(oap->regname, &reglen))
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 Moolenaar6d90c612020-06-29 20:23:32 +02001024 (void)dict_add_string(v_event, "regtype", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001025
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001026 (void)dict_add_bool(v_event, "visual", oap->is_VIsual);
Bram Moolenaar37d16732020-06-12 22:09:01 +02001027
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001028 // Lock the dictionary and its keys
1029 dict_set_items_ro(v_event);
1030
1031 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001032 textwinlock++;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001033 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001034 textwinlock--;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001035 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
1047init_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
1057clear_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
1075free_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 Moolenaar45fffdf2020-03-24 21:42:01 +01001105 void
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001106free_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
1120op_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 Moolenaar32aa1022019-11-02 22:54:41 +01001343 // Some versions of Vi use ">=" here, some don't...
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001344 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 Moolenaare1004402020-10-24 20:49:43 +02001371 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001372 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001373 // 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 Moolenaar4aea03e2019-09-25 22:37:17 +02001381 }
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 Moolenaar37096af2021-03-02 19:04:11 +01001405 // Also copy to the '*' register, in case auto-select is off. But not when
1406 // 'clipboard' has "unnamedplus" and not "unnamed".
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001407 if (clip_plus.available
1408 && (curr == &(y_regs[PLUS_REGISTER])
1409 || (!deleting && oap->regname == 0
1410 && ((clip_unnamed | clip_unnamed_saved) &
Bram Moolenaar37096af2021-03-02 19:04:11 +01001411 CLIP_UNNAMED_PLUS))))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001412 {
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 Moolenaar37096af2021-03-02 19:04:11 +01001419 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 Moolenaar4aea03e2019-09-25 22:37:17 +02001424 {
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
1440fail: // free the allocated lines
1441 free_yank(y_idx + 1);
1442 y_current = curr;
1443 return FAIL;
1444}
1445
1446 static int
1447yank_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
1470copy_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")
Christian Brabandt2fa93842021-05-30 22:17:25 +02001500 * PUT_BLOCK_INNER in block mode, do not add trailing spaces
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001501 */
1502 void
1503do_put(
1504 int regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001505 char_u *expr_result, // result for regname "=" when compiled
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001506 int dir, // BACKWARD for 'P', FORWARD for 'p'
1507 long count,
1508 int flags)
1509{
1510 char_u *ptr;
1511 char_u *newp, *oldp;
1512 int yanklen;
1513 int totlen = 0; // init for gcc
1514 linenr_T lnum;
1515 colnr_T col;
1516 long i; // index in y_array[]
1517 int y_type;
1518 long y_size;
1519 int oldlen;
1520 long y_width = 0;
1521 colnr_T vcol;
1522 int delcount;
1523 int incr = 0;
1524 long j;
1525 struct block_def bd;
1526 char_u **y_array = NULL;
1527 long nr_lines = 0;
1528 pos_T new_cursor;
1529 int indent;
1530 int orig_indent = 0; // init for gcc
1531 int indent_diff = 0; // init for gcc
1532 int first_indent = TRUE;
1533 int lendiff = 0;
1534 pos_T old_pos;
1535 char_u *insert_string = NULL;
1536 int allocated = FALSE;
1537 long cnt;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001538 pos_T orig_start = curbuf->b_op_start;
1539 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001540
1541#ifdef FEAT_CLIPBOARD
1542 // Adjust register name for "unnamed" in 'clipboard'.
1543 adjust_clip_reg(&regname);
1544 (void)may_get_selection(regname);
1545#endif
1546
1547 if (flags & PUT_FIXINDENT)
1548 orig_indent = get_indent();
1549
1550 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1551 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1552
1553 // Using inserted text works differently, because the register includes
1554 // special characters (newlines, etc.).
1555 if (regname == '.')
1556 {
1557 if (VIsual_active)
1558 stuffcharReadbuff(VIsual_mode);
1559 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
1560 (count == -1 ? 'O' : 'i')), count, FALSE);
1561 // Putting the text is done later, so can't really move the cursor to
1562 // the next character. Use "l" to simulate it.
1563 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
1564 stuffcharReadbuff('l');
1565 return;
1566 }
1567
1568 // For special registers '%' (file name), '#' (alternate file name) and
1569 // ':' (last command line), etc. we have to create a fake yank register.
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001570 // For compiled code "expr_result" holds the expression result.
1571 if (regname == '=' && expr_result != NULL)
1572 insert_string = expr_result;
1573 else if (get_spec_reg(regname, &insert_string, &allocated, TRUE)
1574 && insert_string == NULL)
1575 return;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001576
1577 // Autocommands may be executed when saving lines for undo. This might
1578 // make "y_array" invalid, so we start undo now to avoid that.
1579 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
1580 goto end;
1581
1582 if (insert_string != NULL)
1583 {
1584 y_type = MCHAR;
1585#ifdef FEAT_EVAL
1586 if (regname == '=')
1587 {
1588 // For the = register we need to split the string at NL
1589 // characters.
1590 // Loop twice: count the number of lines and save them.
1591 for (;;)
1592 {
1593 y_size = 0;
1594 ptr = insert_string;
1595 while (ptr != NULL)
1596 {
1597 if (y_array != NULL)
1598 y_array[y_size] = ptr;
1599 ++y_size;
1600 ptr = vim_strchr(ptr, '\n');
1601 if (ptr != NULL)
1602 {
1603 if (y_array != NULL)
1604 *ptr = NUL;
1605 ++ptr;
1606 // A trailing '\n' makes the register linewise.
1607 if (*ptr == NUL)
1608 {
1609 y_type = MLINE;
1610 break;
1611 }
1612 }
1613 }
1614 if (y_array != NULL)
1615 break;
1616 y_array = ALLOC_MULT(char_u *, y_size);
1617 if (y_array == NULL)
1618 goto end;
1619 }
1620 }
1621 else
1622#endif
1623 {
1624 y_size = 1; // use fake one-line yank register
1625 y_array = &insert_string;
1626 }
1627 }
1628 else
1629 {
1630 get_yank_register(regname, FALSE);
1631
1632 y_type = y_current->y_type;
1633 y_width = y_current->y_width;
1634 y_size = y_current->y_size;
1635 y_array = y_current->y_array;
1636 }
1637
1638 if (y_type == MLINE)
1639 {
1640 if (flags & PUT_LINE_SPLIT)
1641 {
1642 char_u *p;
1643
1644 // "p" or "P" in Visual mode: split the lines to put the text in
1645 // between.
1646 if (u_save_cursor() == FAIL)
1647 goto end;
1648 p = ml_get_cursor();
1649 if (dir == FORWARD && *p != NUL)
1650 MB_PTR_ADV(p);
1651 ptr = vim_strsave(p);
1652 if (ptr == NULL)
1653 goto end;
1654 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
1655 vim_free(ptr);
1656
1657 oldp = ml_get_curline();
1658 p = oldp + curwin->w_cursor.col;
1659 if (dir == FORWARD && *p != NUL)
1660 MB_PTR_ADV(p);
1661 ptr = vim_strnsave(oldp, p - oldp);
1662 if (ptr == NULL)
1663 goto end;
1664 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
1665 ++nr_lines;
1666 dir = FORWARD;
1667 }
1668 if (flags & PUT_LINE_FORWARD)
1669 {
1670 // Must be "p" for a Visual block, put lines below the block.
1671 curwin->w_cursor = curbuf->b_visual.vi_end;
1672 dir = FORWARD;
1673 }
1674 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1675 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1676 }
1677
1678 if (flags & PUT_LINE) // :put command or "p" in Visual line mode.
1679 y_type = MLINE;
1680
1681 if (y_size == 0 || y_array == NULL)
1682 {
1683 semsg(_("E353: Nothing in register %s"),
1684 regname == 0 ? (char_u *)"\"" : transchar(regname));
1685 goto end;
1686 }
1687
1688 if (y_type == MBLOCK)
1689 {
1690 lnum = curwin->w_cursor.lnum + y_size + 1;
1691 if (lnum > curbuf->b_ml.ml_line_count)
1692 lnum = curbuf->b_ml.ml_line_count + 1;
1693 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
1694 goto end;
1695 }
1696 else if (y_type == MLINE)
1697 {
1698 lnum = curwin->w_cursor.lnum;
1699#ifdef FEAT_FOLDING
1700 // Correct line number for closed fold. Don't move the cursor yet,
1701 // u_save() uses it.
1702 if (dir == BACKWARD)
1703 (void)hasFolding(lnum, &lnum, NULL);
1704 else
1705 (void)hasFolding(lnum, NULL, &lnum);
1706#endif
1707 if (dir == FORWARD)
1708 ++lnum;
1709 // In an empty buffer the empty line is going to be replaced, include
1710 // it in the saved lines.
1711 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
1712 goto end;
1713#ifdef FEAT_FOLDING
1714 if (dir == FORWARD)
1715 curwin->w_cursor.lnum = lnum - 1;
1716 else
1717 curwin->w_cursor.lnum = lnum;
1718 curbuf->b_op_start = curwin->w_cursor; // for mark_adjust()
1719#endif
1720 }
1721 else if (u_save_cursor() == FAIL)
1722 goto end;
1723
1724 yanklen = (int)STRLEN(y_array[0]);
1725
1726 if (ve_flags == VE_ALL && y_type == MCHAR)
1727 {
1728 if (gchar_cursor() == TAB)
1729 {
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001730 int viscol = getviscol();
1731 int ts = curbuf->b_p_ts;
1732
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001733 // Don't need to insert spaces when "p" on the last position of a
1734 // tab or "P" on the first position.
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001735 if (dir == FORWARD ?
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001736#ifdef FEAT_VARTABS
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001737 tabstop_padding(viscol, ts, curbuf->b_p_vts_array) != 1
1738#else
1739 ts - (viscol % ts) != 1
1740#endif
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001741 : curwin->w_cursor.coladd > 0)
1742 coladvance_force(viscol);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001743 else
1744 curwin->w_cursor.coladd = 0;
1745 }
1746 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
1747 coladvance_force(getviscol() + (dir == FORWARD));
1748 }
1749
1750 lnum = curwin->w_cursor.lnum;
1751 col = curwin->w_cursor.col;
1752
1753 // Block mode
1754 if (y_type == MBLOCK)
1755 {
1756 int c = gchar_cursor();
1757 colnr_T endcol2 = 0;
1758
1759 if (dir == FORWARD && c != NUL)
1760 {
1761 if (ve_flags == VE_ALL)
1762 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1763 else
1764 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
1765
1766 if (has_mbyte)
1767 // move to start of next multi-byte character
1768 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
1769 else
1770 if (c != TAB || ve_flags != VE_ALL)
1771 ++curwin->w_cursor.col;
1772 ++col;
1773 }
1774 else
1775 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1776
1777 col += curwin->w_cursor.coladd;
1778 if (ve_flags == VE_ALL
1779 && (curwin->w_cursor.coladd > 0
1780 || endcol2 == curwin->w_cursor.col))
1781 {
1782 if (dir == FORWARD && c == NUL)
1783 ++col;
Bram Moolenaaref85a9b2020-07-10 20:24:07 +02001784 if (dir != FORWARD && c != NUL && curwin->w_cursor.coladd > 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001785 ++curwin->w_cursor.col;
1786 if (c == TAB)
1787 {
1788 if (dir == BACKWARD && curwin->w_cursor.col)
1789 curwin->w_cursor.col--;
1790 if (dir == FORWARD && col - 1 == endcol2)
1791 curwin->w_cursor.col++;
1792 }
1793 }
1794 curwin->w_cursor.coladd = 0;
1795 bd.textcol = 0;
1796 for (i = 0; i < y_size; ++i)
1797 {
Christian Brabandt2fa93842021-05-30 22:17:25 +02001798 int spaces = 0;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001799 char shortline;
1800
1801 bd.startspaces = 0;
1802 bd.endspaces = 0;
1803 vcol = 0;
1804 delcount = 0;
1805
1806 // add a new line
1807 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1808 {
1809 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
1810 (colnr_T)1, FALSE) == FAIL)
1811 break;
1812 ++nr_lines;
1813 }
1814 // get the old line and advance to the position to insert at
1815 oldp = ml_get_curline();
1816 oldlen = (int)STRLEN(oldp);
1817 for (ptr = oldp; vcol < col && *ptr; )
1818 {
1819 // Count a tab for what it's worth (if list mode not on)
1820 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
1821 vcol += incr;
1822 }
1823 bd.textcol = (colnr_T)(ptr - oldp);
1824
1825 shortline = (vcol < col) || (vcol == col && !*ptr) ;
1826
1827 if (vcol < col) // line too short, padd with spaces
1828 bd.startspaces = col - vcol;
1829 else if (vcol > col)
1830 {
1831 bd.endspaces = vcol - col;
1832 bd.startspaces = incr - bd.endspaces;
1833 --bd.textcol;
1834 delcount = 1;
1835 if (has_mbyte)
1836 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
1837 if (oldp[bd.textcol] != TAB)
1838 {
1839 // Only a Tab can be split into spaces. Other
1840 // characters will have to be moved to after the
1841 // block, causing misalignment.
1842 delcount = 0;
1843 bd.endspaces = 0;
1844 }
1845 }
1846
1847 yanklen = (int)STRLEN(y_array[i]);
1848
Christian Brabandt2fa93842021-05-30 22:17:25 +02001849 if ((flags & PUT_BLOCK_INNER) == 0)
1850 {
1851 // calculate number of spaces required to fill right side of
1852 // block
1853 spaces = y_width + 1;
1854 for (j = 0; j < yanklen; j++)
1855 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
1856 if (spaces < 0)
1857 spaces = 0;
1858 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001859
1860 // insert the new text
1861 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
1862 newp = alloc(totlen + oldlen + 1);
1863 if (newp == NULL)
1864 break;
1865 // copy part up to cursor to new line
1866 ptr = newp;
1867 mch_memmove(ptr, oldp, (size_t)bd.textcol);
1868 ptr += bd.textcol;
1869 // may insert some spaces before the new text
1870 vim_memset(ptr, ' ', (size_t)bd.startspaces);
1871 ptr += bd.startspaces;
1872 // insert the new text
1873 for (j = 0; j < count; ++j)
1874 {
1875 mch_memmove(ptr, y_array[i], (size_t)yanklen);
1876 ptr += yanklen;
1877
1878 // insert block's trailing spaces only if there's text behind
1879 if ((j < count - 1 || !shortline) && spaces)
1880 {
1881 vim_memset(ptr, ' ', (size_t)spaces);
1882 ptr += spaces;
1883 }
1884 }
1885 // may insert some spaces after the new text
1886 vim_memset(ptr, ' ', (size_t)bd.endspaces);
1887 ptr += bd.endspaces;
1888 // move the text after the cursor to the end of the line.
1889 mch_memmove(ptr, oldp + bd.textcol + delcount,
1890 (size_t)(oldlen - bd.textcol - delcount + 1));
1891 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
1892
1893 ++curwin->w_cursor.lnum;
1894 if (i == 0)
1895 curwin->w_cursor.col += bd.startspaces;
1896 }
1897
1898 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
1899
1900 // Set '[ mark.
1901 curbuf->b_op_start = curwin->w_cursor;
1902 curbuf->b_op_start.lnum = lnum;
1903
1904 // adjust '] mark
1905 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
1906 curbuf->b_op_end.col = bd.textcol + totlen - 1;
1907 curbuf->b_op_end.coladd = 0;
1908 if (flags & PUT_CURSEND)
1909 {
1910 colnr_T len;
1911
1912 curwin->w_cursor = curbuf->b_op_end;
1913 curwin->w_cursor.col++;
1914
1915 // in Insert mode we might be after the NUL, correct for that
1916 len = (colnr_T)STRLEN(ml_get_curline());
1917 if (curwin->w_cursor.col > len)
1918 curwin->w_cursor.col = len;
1919 }
1920 else
1921 curwin->w_cursor.lnum = lnum;
1922 }
1923 else
1924 {
1925 // Character or Line mode
1926 if (y_type == MCHAR)
1927 {
1928 // if type is MCHAR, FORWARD is the same as BACKWARD on the next
1929 // char
1930 if (dir == FORWARD && gchar_cursor() != NUL)
1931 {
1932 if (has_mbyte)
1933 {
1934 int bytelen = (*mb_ptr2len)(ml_get_cursor());
1935
1936 // put it on the next of the multi-byte character.
1937 col += bytelen;
1938 if (yanklen)
1939 {
1940 curwin->w_cursor.col += bytelen;
1941 curbuf->b_op_end.col += bytelen;
1942 }
1943 }
1944 else
1945 {
1946 ++col;
1947 if (yanklen)
1948 {
1949 ++curwin->w_cursor.col;
1950 ++curbuf->b_op_end.col;
1951 }
1952 }
1953 }
1954 curbuf->b_op_start = curwin->w_cursor;
1955 }
1956 // Line mode: BACKWARD is the same as FORWARD on the previous line
1957 else if (dir == BACKWARD)
1958 --lnum;
1959 new_cursor = curwin->w_cursor;
1960
Bram Moolenaarcd942772020-08-22 21:08:44 +02001961 // simple case: insert into one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001962 if (y_type == MCHAR && y_size == 1)
1963 {
Bram Moolenaarcd942772020-08-22 21:08:44 +02001964 linenr_T end_lnum = 0; // init for gcc
1965 linenr_T start_lnum = lnum;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001966
1967 if (VIsual_active)
1968 {
1969 end_lnum = curbuf->b_visual.vi_end.lnum;
1970 if (end_lnum < curbuf->b_visual.vi_start.lnum)
1971 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaarcd942772020-08-22 21:08:44 +02001972 if (end_lnum > start_lnum)
1973 {
1974 pos_T pos;
1975
1976 // "col" is valid for the first line, in following lines
1977 // the virtual column needs to be used. Matters for
1978 // multi-byte characters.
1979 pos.lnum = lnum;
1980 pos.col = col;
1981 pos.coladd = 0;
1982 getvcol(curwin, &pos, NULL, &vcol, NULL);
1983 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001984 }
1985
1986 do {
1987 totlen = count * yanklen;
1988 if (totlen > 0)
1989 {
1990 oldp = ml_get(lnum);
Bram Moolenaarcd942772020-08-22 21:08:44 +02001991 if (lnum > start_lnum)
1992 {
1993 pos_T pos;
1994
1995 pos.lnum = lnum;
1996 if (getvpos(&pos, vcol) == OK)
1997 col = pos.col;
1998 else
1999 col = MAXCOL;
2000 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002001 if (VIsual_active && col > (int)STRLEN(oldp))
2002 {
2003 lnum++;
2004 continue;
2005 }
2006 newp = alloc(STRLEN(oldp) + totlen + 1);
2007 if (newp == NULL)
2008 goto end; // alloc() gave an error message
2009 mch_memmove(newp, oldp, (size_t)col);
2010 ptr = newp + col;
2011 for (i = 0; i < count; ++i)
2012 {
2013 mch_memmove(ptr, y_array[0], (size_t)yanklen);
2014 ptr += yanklen;
2015 }
2016 STRMOVE(ptr, oldp + col);
2017 ml_replace(lnum, newp, FALSE);
2018 // Place cursor on last putted char.
2019 if (lnum == curwin->w_cursor.lnum)
2020 {
2021 // make sure curwin->w_virtcol is updated
2022 changed_cline_bef_curs();
2023 curwin->w_cursor.col += (colnr_T)(totlen - 1);
2024 }
2025 }
2026 if (VIsual_active)
2027 lnum++;
2028 } while (VIsual_active && lnum <= end_lnum);
2029
2030 if (VIsual_active) // reset lnum to the last visual line
2031 lnum--;
2032
2033 curbuf->b_op_end = curwin->w_cursor;
2034 // For "CTRL-O p" in Insert mode, put cursor after last char
2035 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
2036 ++curwin->w_cursor.col;
2037 changed_bytes(lnum, col);
2038 }
2039 else
2040 {
2041 // Insert at least one line. When y_type is MCHAR, break the first
2042 // line in two.
2043 for (cnt = 1; cnt <= count; ++cnt)
2044 {
2045 i = 0;
2046 if (y_type == MCHAR)
2047 {
2048 // Split the current line in two at the insert position.
2049 // First insert y_array[size - 1] in front of second line.
2050 // Then append y_array[0] to first line.
2051 lnum = new_cursor.lnum;
2052 ptr = ml_get(lnum) + col;
2053 totlen = (int)STRLEN(y_array[y_size - 1]);
2054 newp = alloc(STRLEN(ptr) + totlen + 1);
2055 if (newp == NULL)
2056 goto error;
2057 STRCPY(newp, y_array[y_size - 1]);
2058 STRCAT(newp, ptr);
2059 // insert second line
2060 ml_append(lnum, newp, (colnr_T)0, FALSE);
2061 vim_free(newp);
2062
2063 oldp = ml_get(lnum);
2064 newp = alloc(col + yanklen + 1);
2065 if (newp == NULL)
2066 goto error;
2067 // copy first part of line
2068 mch_memmove(newp, oldp, (size_t)col);
2069 // append to first line
2070 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
2071 ml_replace(lnum, newp, FALSE);
2072
2073 curwin->w_cursor.lnum = lnum;
2074 i = 1;
2075 }
2076
2077 for (; i < y_size; ++i)
2078 {
2079 if ((y_type != MCHAR || i < y_size - 1)
2080 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
2081 == FAIL)
2082 goto error;
2083 lnum++;
2084 ++nr_lines;
2085 if (flags & PUT_FIXINDENT)
2086 {
2087 old_pos = curwin->w_cursor;
2088 curwin->w_cursor.lnum = lnum;
2089 ptr = ml_get(lnum);
2090 if (cnt == count && i == y_size - 1)
2091 lendiff = (int)STRLEN(ptr);
2092#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
2093 if (*ptr == '#' && preprocs_left())
2094 indent = 0; // Leave # lines at start
2095 else
2096#endif
2097 if (*ptr == NUL)
2098 indent = 0; // Ignore empty lines
2099 else if (first_indent)
2100 {
2101 indent_diff = orig_indent - get_indent();
2102 indent = orig_indent;
2103 first_indent = FALSE;
2104 }
2105 else if ((indent = get_indent() + indent_diff) < 0)
2106 indent = 0;
2107 (void)set_indent(indent, 0);
2108 curwin->w_cursor = old_pos;
2109 // remember how many chars were removed
2110 if (cnt == count && i == y_size - 1)
2111 lendiff -= (int)STRLEN(ml_get(lnum));
2112 }
2113 }
2114 }
2115
2116error:
2117 // Adjust marks.
2118 if (y_type == MLINE)
2119 {
2120 curbuf->b_op_start.col = 0;
2121 if (dir == FORWARD)
2122 curbuf->b_op_start.lnum++;
2123 }
2124 // Skip mark_adjust when adding lines after the last one, there
2125 // can't be marks there. But still needed in diff mode.
2126 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
2127 < curbuf->b_ml.ml_line_count
2128#ifdef FEAT_DIFF
2129 || curwin->w_p_diff
2130#endif
2131 )
2132 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
2133 (linenr_T)MAXLNUM, nr_lines, 0L);
2134
2135 // note changed text for displaying and folding
2136 if (y_type == MCHAR)
2137 changed_lines(curwin->w_cursor.lnum, col,
2138 curwin->w_cursor.lnum + 1, nr_lines);
2139 else
2140 changed_lines(curbuf->b_op_start.lnum, 0,
2141 curbuf->b_op_start.lnum, nr_lines);
2142
2143 // put '] mark at last inserted character
2144 curbuf->b_op_end.lnum = lnum;
2145 // correct length for change in indent
2146 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
2147 if (col > 1)
2148 curbuf->b_op_end.col = col - 1;
2149 else
2150 curbuf->b_op_end.col = 0;
2151
2152 if (flags & PUT_CURSLINE)
2153 {
2154 // ":put": put cursor on last inserted line
2155 curwin->w_cursor.lnum = lnum;
2156 beginline(BL_WHITE | BL_FIX);
2157 }
2158 else if (flags & PUT_CURSEND)
2159 {
2160 // put cursor after inserted text
2161 if (y_type == MLINE)
2162 {
2163 if (lnum >= curbuf->b_ml.ml_line_count)
2164 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2165 else
2166 curwin->w_cursor.lnum = lnum + 1;
2167 curwin->w_cursor.col = 0;
2168 }
2169 else
2170 {
2171 curwin->w_cursor.lnum = lnum;
2172 curwin->w_cursor.col = col;
2173 }
2174 }
2175 else if (y_type == MLINE)
2176 {
2177 // put cursor on first non-blank in first inserted line
2178 curwin->w_cursor.col = 0;
2179 if (dir == FORWARD)
2180 ++curwin->w_cursor.lnum;
2181 beginline(BL_WHITE | BL_FIX);
2182 }
2183 else // put cursor on first inserted character
2184 curwin->w_cursor = new_cursor;
2185 }
2186 }
2187
2188 msgmore(nr_lines);
2189 curwin->w_set_curswant = TRUE;
2190
2191end:
Bram Moolenaare1004402020-10-24 20:49:43 +02002192 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002193 {
2194 curbuf->b_op_start = orig_start;
2195 curbuf->b_op_end = orig_end;
2196 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002197 if (allocated)
2198 vim_free(insert_string);
2199 if (regname == '=')
2200 vim_free(y_array);
2201
2202 VIsual_active = FALSE;
2203
2204 // If the cursor is past the end of the line put it at the end.
2205 adjust_cursor_eol();
2206}
2207
2208/*
2209 * Return the character name of the register with the given number.
2210 */
2211 int
2212get_register_name(int num)
2213{
2214 if (num == -1)
2215 return '"';
2216 else if (num < 10)
2217 return num + '0';
2218 else if (num == DELETION_REGISTER)
2219 return '-';
2220#ifdef FEAT_CLIPBOARD
2221 else if (num == STAR_REGISTER)
2222 return '*';
2223 else if (num == PLUS_REGISTER)
2224 return '+';
2225#endif
2226 else
2227 {
2228#ifdef EBCDIC
2229 int i;
2230
2231 // EBCDIC is really braindead ...
2232 i = 'a' + (num - 10);
2233 if (i > 'i')
2234 i += 7;
2235 if (i > 'r')
2236 i += 8;
2237 return i;
2238#else
2239 return num + 'a' - 10;
2240#endif
2241 }
2242}
2243
2244/*
Bram Moolenaarbb861e22020-06-07 18:16:36 +02002245 * Return the index of the register "" points to.
2246 */
2247 int
2248get_unname_register()
2249{
2250 return y_previous == NULL ? -1 : y_previous - &y_regs[0];
2251}
2252
2253/*
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002254 * ":dis" and ":registers": Display the contents of the yank registers.
2255 */
2256 void
2257ex_display(exarg_T *eap)
2258{
2259 int i, n;
2260 long j;
2261 char_u *p;
2262 yankreg_T *yb;
2263 int name;
2264 int attr;
2265 char_u *arg = eap->arg;
2266 int clen;
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002267 int type;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002268
2269 if (arg != NULL && *arg == NUL)
2270 arg = NULL;
2271 attr = HL_ATTR(HLF_8);
2272
2273 // Highlight title
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002274 msg_puts_title(_("\nType Name Content"));
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002275 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
2276 {
2277 name = get_register_name(i);
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002278 switch (get_reg_type(name, NULL))
2279 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002280 case MLINE: type = 'l'; break;
2281 case MCHAR: type = 'c'; break;
2282 default: type = 'b'; break;
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002283 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002284 if (arg != NULL && vim_strchr(arg, name) == NULL
2285#ifdef ONE_CLIPBOARD
2286 // Star register and plus register contain the same thing.
2287 && (name != '*' || vim_strchr(arg, '+') == NULL)
2288#endif
2289 )
2290 continue; // did not ask for this register
2291
2292#ifdef FEAT_CLIPBOARD
2293 // Adjust register name for "unnamed" in 'clipboard'.
2294 // When it's a clipboard register, fill it with the current contents
2295 // of the clipboard.
2296 adjust_clip_reg(&name);
2297 (void)may_get_selection(name);
2298#endif
2299
2300 if (i == -1)
2301 {
2302 if (y_previous != NULL)
2303 yb = y_previous;
2304 else
2305 yb = &(y_regs[0]);
2306 }
2307 else
2308 yb = &(y_regs[i]);
2309
2310#ifdef FEAT_EVAL
2311 if (name == MB_TOLOWER(redir_reg)
2312 || (redir_reg == '"' && yb == y_previous))
2313 continue; // do not list register being written to, the
2314 // pointer can be freed
2315#endif
2316
2317 if (yb->y_array != NULL)
2318 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002319 int do_show = FALSE;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002320
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002321 for (j = 0; !do_show && j < yb->y_size; ++j)
2322 do_show = !message_filtered(yb->y_array[j]);
2323
2324 if (do_show || yb->y_size == 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002325 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002326 msg_putchar('\n');
2327 msg_puts(" ");
2328 msg_putchar(type);
2329 msg_puts(" ");
2330 msg_putchar('"');
2331 msg_putchar(name);
2332 msg_puts(" ");
2333
2334 n = (int)Columns - 11;
2335 for (j = 0; j < yb->y_size && n > 1; ++j)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002336 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002337 if (j)
2338 {
2339 msg_puts_attr("^J", attr);
2340 n -= 2;
2341 }
2342 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0;
2343 ++p)
2344 {
2345 clen = (*mb_ptr2len)(p);
2346 msg_outtrans_len(p, clen);
2347 p += clen - 1;
2348 }
2349 }
2350 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002351 msg_puts_attr("^J", attr);
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002352 out_flush(); // show one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002353 }
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002354 ui_breakcheck();
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002355 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002356 }
2357
2358 // display last inserted text
2359 if ((p = get_last_insert()) != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002360 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int
2361 && !message_filtered(p))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002362 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002363 msg_puts("\n c \". ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002364 dis_msg(p, TRUE);
2365 }
2366
2367 // display last command line
2368 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002369 && !got_int && !message_filtered(last_cmdline))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002370 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002371 msg_puts("\n c \": ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002372 dis_msg(last_cmdline, FALSE);
2373 }
2374
2375 // display current file name
2376 if (curbuf->b_fname != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002377 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int
2378 && !message_filtered(curbuf->b_fname))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002379 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002380 msg_puts("\n c \"% ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002381 dis_msg(curbuf->b_fname, FALSE);
2382 }
2383
2384 // display alternate file name
2385 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
2386 {
2387 char_u *fname;
2388 linenr_T dummy;
2389
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002390 if (buflist_name_nr(0, &fname, &dummy) != FAIL
2391 && !message_filtered(fname))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002392 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002393 msg_puts("\n c \"# ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002394 dis_msg(fname, FALSE);
2395 }
2396 }
2397
2398 // display last search pattern
2399 if (last_search_pat() != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002400 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int
2401 && !message_filtered(last_search_pat()))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002402 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002403 msg_puts("\n c \"/ ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002404 dis_msg(last_search_pat(), FALSE);
2405 }
2406
2407#ifdef FEAT_EVAL
2408 // display last used expression
2409 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002410 && !got_int && !message_filtered(expr_line))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002411 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002412 msg_puts("\n c \"= ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002413 dis_msg(expr_line, FALSE);
2414 }
2415#endif
2416}
2417
2418/*
2419 * display a string for do_dis()
2420 * truncate at end of screen line
2421 */
2422 static void
2423dis_msg(
2424 char_u *p,
2425 int skip_esc) // if TRUE, ignore trailing ESC
2426{
2427 int n;
2428 int l;
2429
2430 n = (int)Columns - 6;
2431 while (*p != NUL
2432 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
2433 && (n -= ptr2cells(p)) >= 0)
2434 {
2435 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
2436 {
2437 msg_outtrans_len(p, l);
2438 p += l;
2439 }
2440 else
2441 msg_outtrans_len(p++, 1);
2442 }
2443 ui_breakcheck();
2444}
2445
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002446#if defined(FEAT_DND) || defined(PROTO)
2447/*
2448 * Replace the contents of the '~' register with str.
2449 */
2450 void
2451dnd_yank_drag_data(char_u *str, long len)
2452{
2453 yankreg_T *curr;
2454
2455 curr = y_current;
2456 y_current = &y_regs[TILDE_REGISTER];
2457 free_yank_all();
2458 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
2459 y_current = curr;
2460}
2461#endif
2462
2463
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002464/*
2465 * Return the type of a register.
2466 * Used for getregtype()
2467 * Returns MAUTO for error.
2468 */
2469 char_u
2470get_reg_type(int regname, long *reglen)
2471{
2472 switch (regname)
2473 {
2474 case '%': // file name
2475 case '#': // alternate file name
2476 case '=': // expression
2477 case ':': // last command line
2478 case '/': // last search-pattern
2479 case '.': // last inserted text
2480# ifdef FEAT_SEARCHPATH
2481 case Ctrl_F: // Filename under cursor
2482 case Ctrl_P: // Path under cursor, expand via "path"
2483# endif
2484 case Ctrl_W: // word under cursor
2485 case Ctrl_A: // WORD (mnemonic All) under cursor
2486 case '_': // black hole: always empty
2487 return MCHAR;
2488 }
2489
2490# ifdef FEAT_CLIPBOARD
2491 regname = may_get_selection(regname);
2492# endif
2493
2494 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2495 return MAUTO;
2496
2497 get_yank_register(regname, FALSE);
2498
2499 if (y_current->y_array != NULL)
2500 {
2501 if (reglen != NULL && y_current->y_type == MBLOCK)
2502 *reglen = y_current->y_width;
2503 return y_current->y_type;
2504 }
2505 return MAUTO;
2506}
2507
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002508#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002509/*
2510 * When "flags" has GREG_LIST return a list with text "s".
2511 * Otherwise just return "s".
2512 */
2513 static char_u *
2514getreg_wrap_one_line(char_u *s, int flags)
2515{
2516 if (flags & GREG_LIST)
2517 {
2518 list_T *list = list_alloc();
2519
2520 if (list != NULL)
2521 {
2522 if (list_append_string(list, NULL, -1) == FAIL)
2523 {
2524 list_free(list);
2525 return NULL;
2526 }
2527 list->lv_first->li_tv.vval.v_string = s;
2528 }
2529 return (char_u *)list;
2530 }
2531 return s;
2532}
2533
2534/*
Bram Moolenaard672dde2020-02-26 13:43:51 +01002535 * Return the contents of a register as a single allocated string or as a list.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002536 * Used for "@r" in expressions and for getreg().
2537 * Returns NULL for error.
2538 * Flags:
2539 * GREG_NO_EXPR Do not allow expression register
2540 * GREG_EXPR_SRC For the expression register: return expression itself,
2541 * not the result of its evaluation.
Bram Moolenaard672dde2020-02-26 13:43:51 +01002542 * GREG_LIST Return a list of lines instead of a single string.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002543 */
2544 char_u *
2545get_reg_contents(int regname, int flags)
2546{
2547 long i;
2548 char_u *retval;
2549 int allocated;
2550 long len;
2551
2552 // Don't allow using an expression register inside an expression
2553 if (regname == '=')
2554 {
2555 if (flags & GREG_NO_EXPR)
2556 return NULL;
2557 if (flags & GREG_EXPR_SRC)
2558 return getreg_wrap_one_line(get_expr_line_src(), flags);
2559 return getreg_wrap_one_line(get_expr_line(), flags);
2560 }
2561
2562 if (regname == '@') // "@@" is used for unnamed register
2563 regname = '"';
2564
2565 // check for valid regname
2566 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2567 return NULL;
2568
2569# ifdef FEAT_CLIPBOARD
2570 regname = may_get_selection(regname);
2571# endif
2572
2573 if (get_spec_reg(regname, &retval, &allocated, FALSE))
2574 {
2575 if (retval == NULL)
2576 return NULL;
2577 if (allocated)
2578 return getreg_wrap_one_line(retval, flags);
2579 return getreg_wrap_one_line(vim_strsave(retval), flags);
2580 }
2581
2582 get_yank_register(regname, FALSE);
2583 if (y_current->y_array == NULL)
2584 return NULL;
2585
2586 if (flags & GREG_LIST)
2587 {
2588 list_T *list = list_alloc();
2589 int error = FALSE;
2590
2591 if (list == NULL)
2592 return NULL;
2593 for (i = 0; i < y_current->y_size; ++i)
2594 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
2595 error = TRUE;
2596 if (error)
2597 {
2598 list_free(list);
2599 return NULL;
2600 }
2601 return (char_u *)list;
2602 }
2603
2604 // Compute length of resulting string.
2605 len = 0;
2606 for (i = 0; i < y_current->y_size; ++i)
2607 {
2608 len += (long)STRLEN(y_current->y_array[i]);
2609 // Insert a newline between lines and after last line if
2610 // y_type is MLINE.
2611 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2612 ++len;
2613 }
2614
2615 retval = alloc(len + 1);
2616
2617 // Copy the lines of the yank register into the string.
2618 if (retval != NULL)
2619 {
2620 len = 0;
2621 for (i = 0; i < y_current->y_size; ++i)
2622 {
2623 STRCPY(retval + len, y_current->y_array[i]);
2624 len += (long)STRLEN(retval + len);
2625
2626 // Insert a NL between lines and after the last line if y_type is
2627 // MLINE.
2628 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2629 retval[len++] = '\n';
2630 }
2631 retval[len] = NUL;
2632 }
2633
2634 return retval;
2635}
2636
2637 static int
2638init_write_reg(
2639 int name,
2640 yankreg_T **old_y_previous,
2641 yankreg_T **old_y_current,
2642 int must_append,
2643 int *yank_type UNUSED)
2644{
2645 if (!valid_yank_reg(name, TRUE)) // check for valid reg name
2646 {
2647 emsg_invreg(name);
2648 return FAIL;
2649 }
2650
2651 // Don't want to change the current (unnamed) register
2652 *old_y_previous = y_previous;
2653 *old_y_current = y_current;
2654
2655 get_yank_register(name, TRUE);
2656 if (!y_append && !must_append)
2657 free_yank_all();
2658 return OK;
2659}
2660
2661 static void
2662finish_write_reg(
2663 int name,
2664 yankreg_T *old_y_previous,
2665 yankreg_T *old_y_current)
2666{
2667# ifdef FEAT_CLIPBOARD
2668 // Send text of clipboard register to the clipboard.
2669 may_set_selection();
2670# endif
2671
2672 // ':let @" = "val"' should change the meaning of the "" register
2673 if (name != '"')
2674 y_previous = old_y_previous;
2675 y_current = old_y_current;
2676}
2677
2678/*
2679 * Store string "str" in register "name".
2680 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
2681 * If "must_append" is TRUE, always append to the register. Otherwise append
2682 * if "name" is an uppercase letter.
2683 * Note: "maxlen" and "must_append" don't work for the "/" register.
2684 * Careful: 'str' is modified, you may have to use a copy!
2685 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
2686 */
2687 void
2688write_reg_contents(
2689 int name,
2690 char_u *str,
2691 int maxlen,
2692 int must_append)
2693{
2694 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
2695}
2696
2697 void
2698write_reg_contents_lst(
2699 int name,
2700 char_u **strings,
2701 int maxlen UNUSED,
2702 int must_append,
2703 int yank_type,
2704 long block_len)
2705{
2706 yankreg_T *old_y_previous, *old_y_current;
2707
2708 if (name == '/' || name == '=')
2709 {
2710 char_u *s;
2711
2712 if (strings[0] == NULL)
2713 s = (char_u *)"";
2714 else if (strings[1] != NULL)
2715 {
2716 emsg(_("E883: search pattern and expression register may not "
2717 "contain two or more lines"));
2718 return;
2719 }
2720 else
2721 s = strings[0];
2722 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
2723 return;
2724 }
2725
2726 if (name == '_') // black hole: nothing to do
2727 return;
2728
2729 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2730 &yank_type) == FAIL)
2731 return;
2732
2733 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
2734
2735 finish_write_reg(name, old_y_previous, old_y_current);
2736}
2737
2738 void
2739write_reg_contents_ex(
2740 int name,
2741 char_u *str,
2742 int maxlen,
2743 int must_append,
2744 int yank_type,
2745 long block_len)
2746{
2747 yankreg_T *old_y_previous, *old_y_current;
2748 long len;
2749
2750 if (maxlen >= 0)
2751 len = maxlen;
2752 else
2753 len = (long)STRLEN(str);
2754
2755 // Special case: '/' search pattern
2756 if (name == '/')
2757 {
2758 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
2759 return;
2760 }
2761
2762 if (name == '#')
2763 {
2764 buf_T *buf;
2765
2766 if (VIM_ISDIGIT(*str))
2767 {
2768 int num = atoi((char *)str);
2769
2770 buf = buflist_findnr(num);
2771 if (buf == NULL)
2772 semsg(_(e_nobufnr), (long)num);
2773 }
2774 else
2775 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
2776 TRUE, FALSE, FALSE));
2777 if (buf == NULL)
2778 return;
2779 curwin->w_alt_fnum = buf->b_fnum;
2780 return;
2781 }
2782
2783 if (name == '=')
2784 {
2785 char_u *p, *s;
2786
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002787 p = vim_strnsave(str, len);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002788 if (p == NULL)
2789 return;
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002790 if (must_append && expr_line != NULL)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002791 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002792 s = concat_str(expr_line, p);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002793 vim_free(p);
2794 p = s;
2795 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002796 set_expr_line(p, NULL);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002797 return;
2798 }
2799
2800 if (name == '_') // black hole: nothing to do
2801 return;
2802
2803 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2804 &yank_type) == FAIL)
2805 return;
2806
2807 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
2808
2809 finish_write_reg(name, old_y_previous, old_y_current);
2810}
2811#endif // FEAT_EVAL
2812
2813#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
2814/*
2815 * Put a string into a register. When the register is not empty, the string
2816 * is appended.
2817 */
Bram Moolenaar45fffdf2020-03-24 21:42:01 +01002818 void
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002819str_to_reg(
2820 yankreg_T *y_ptr, // pointer to yank register
2821 int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO
2822 char_u *str, // string to put in register
2823 long len, // length of string
2824 long blocklen, // width of Visual block
2825 int str_list) // TRUE if str is char_u **
2826{
2827 int type; // MCHAR, MLINE or MBLOCK
2828 int lnum;
2829 long start;
2830 long i;
2831 int extra;
2832 int newlines; // number of lines added
2833 int extraline = 0; // extra line at the end
2834 int append = FALSE; // append to last line in register
2835 char_u *s;
2836 char_u **ss;
2837 char_u **pp;
2838 long maxlen;
2839
2840 if (y_ptr->y_array == NULL) // NULL means empty register
2841 y_ptr->y_size = 0;
2842
2843 if (yank_type == MAUTO)
2844 type = ((str_list || (len > 0 && (str[len - 1] == NL
2845 || str[len - 1] == CAR)))
2846 ? MLINE : MCHAR);
2847 else
2848 type = yank_type;
2849
2850 // Count the number of lines within the string
2851 newlines = 0;
2852 if (str_list)
2853 {
2854 for (ss = (char_u **) str; *ss != NULL; ++ss)
2855 ++newlines;
2856 }
2857 else
2858 {
2859 for (i = 0; i < len; i++)
2860 if (str[i] == '\n')
2861 ++newlines;
2862 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
2863 {
2864 extraline = 1;
2865 ++newlines; // count extra newline at the end
2866 }
2867 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
2868 {
2869 append = TRUE;
2870 --newlines; // uncount newline when appending first line
2871 }
2872 }
2873
2874 // Without any lines make the register empty.
2875 if (y_ptr->y_size + newlines == 0)
2876 {
2877 VIM_CLEAR(y_ptr->y_array);
2878 return;
2879 }
2880
2881 // Allocate an array to hold the pointers to the new register lines.
2882 // If the register was not empty, move the existing lines to the new array.
2883 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
2884 if (pp == NULL) // out of memory
2885 return;
2886 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
2887 pp[lnum] = y_ptr->y_array[lnum];
2888 vim_free(y_ptr->y_array);
2889 y_ptr->y_array = pp;
2890 maxlen = 0;
2891
2892 // Find the end of each line and save it into the array.
2893 if (str_list)
2894 {
2895 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
2896 {
Bram Moolenaar6c4c4042021-06-04 19:17:07 +02002897 pp[lnum] = vim_strsave(*ss);
2898 if (type == MBLOCK)
2899 {
2900 int charlen = mb_string2cells(*ss, -1);
2901
2902 if (charlen > maxlen)
2903 maxlen = charlen;
2904 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002905 }
2906 }
2907 else
2908 {
2909 for (start = 0; start < len + extraline; start += i + 1)
2910 {
Bram Moolenaar6c4c4042021-06-04 19:17:07 +02002911 int charlen = 0;
2912
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002913 for (i = start; i < len; ++i) // find the end of the line
Bram Moolenaar24951a62021-06-04 18:33:49 +02002914 {
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002915 if (str[i] == '\n')
2916 break;
Bram Moolenaar6c4c4042021-06-04 19:17:07 +02002917 if (type == MBLOCK)
2918 charlen += mb_ptr2cells_len(str + i, len - i);
Bram Moolenaar24951a62021-06-04 18:33:49 +02002919 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002920 i -= start; // i is now length of line
Bram Moolenaar6e0b5532021-06-04 17:11:47 +02002921 if (charlen > maxlen)
2922 maxlen = charlen;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002923 if (append)
2924 {
2925 --lnum;
2926 extra = (int)STRLEN(y_ptr->y_array[lnum]);
2927 }
2928 else
2929 extra = 0;
2930 s = alloc(i + extra + 1);
2931 if (s == NULL)
2932 break;
2933 if (extra)
2934 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
2935 if (append)
2936 vim_free(y_ptr->y_array[lnum]);
Bram Moolenaar24951a62021-06-04 18:33:49 +02002937 if (i > 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002938 mch_memmove(s + extra, str + start, (size_t)i);
2939 extra += i;
2940 s[extra] = NUL;
2941 y_ptr->y_array[lnum++] = s;
2942 while (--extra >= 0)
2943 {
2944 if (*s == NUL)
2945 *s = '\n'; // replace NUL with newline
2946 ++s;
2947 }
2948 append = FALSE; // only first line is appended
2949 }
2950 }
2951 y_ptr->y_type = type;
2952 y_ptr->y_size = lnum;
2953 if (type == MBLOCK)
2954 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
2955 else
2956 y_ptr->y_width = 0;
2957# ifdef FEAT_VIMINFO
2958 y_ptr->y_time_set = vim_time();
2959# endif
2960}
2961#endif // FEAT_CLIPBOARD || FEAT_EVAL || PROTO