blob: 6574432bdbecfd8683b09b6b5957208c498b3a34 [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 {
812 stuffescaped(y_current->y_array[i], literally);
813 // Insert a newline between lines and after last line if
814 // y_type is MLINE.
815 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
816 stuffcharReadbuff('\n');
817 }
818 }
819 }
820
821 return retval;
822}
823
824/*
825 * If "regname" is a special register, return TRUE and store a pointer to its
826 * value in "argp".
827 */
828 int
829get_spec_reg(
830 int regname,
831 char_u **argp,
832 int *allocated, // return: TRUE when value was allocated
833 int errmsg) // give error message when failing
834{
835 int cnt;
836
837 *argp = NULL;
838 *allocated = FALSE;
839 switch (regname)
840 {
841 case '%': // file name
842 if (errmsg)
843 check_fname(); // will give emsg if not set
844 *argp = curbuf->b_fname;
845 return TRUE;
846
847 case '#': // alternate file name
848 *argp = getaltfname(errmsg); // may give emsg if not set
849 return TRUE;
850
851#ifdef FEAT_EVAL
852 case '=': // result of expression
853 *argp = get_expr_line();
854 *allocated = TRUE;
855 return TRUE;
856#endif
857
858 case ':': // last command line
859 if (last_cmdline == NULL && errmsg)
860 emsg(_(e_nolastcmd));
861 *argp = last_cmdline;
862 return TRUE;
863
864 case '/': // last search-pattern
865 if (last_search_pat() == NULL && errmsg)
866 emsg(_(e_noprevre));
867 *argp = last_search_pat();
868 return TRUE;
869
870 case '.': // last inserted text
871 *argp = get_last_insert_save();
872 *allocated = TRUE;
873 if (*argp == NULL && errmsg)
874 emsg(_(e_noinstext));
875 return TRUE;
876
877#ifdef FEAT_SEARCHPATH
878 case Ctrl_F: // Filename under cursor
879 case Ctrl_P: // Path under cursor, expand via "path"
880 if (!errmsg)
881 return FALSE;
882 *argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
883 | (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
884 *allocated = TRUE;
885 return TRUE;
886#endif
887
888 case Ctrl_W: // word under cursor
889 case Ctrl_A: // WORD (mnemonic All) under cursor
890 if (!errmsg)
891 return FALSE;
892 cnt = find_ident_under_cursor(argp, regname == Ctrl_W
893 ? (FIND_IDENT|FIND_STRING) : FIND_STRING);
894 *argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
895 *allocated = TRUE;
896 return TRUE;
897
898 case Ctrl_L: // Line under cursor
899 if (!errmsg)
900 return FALSE;
901
902 *argp = ml_get_buf(curwin->w_buffer,
903 curwin->w_cursor.lnum, FALSE);
904 return TRUE;
905
906 case '_': // black hole: always empty
907 *argp = (char_u *)"";
908 return TRUE;
909 }
910
911 return FALSE;
912}
913
914/*
915 * Paste a yank register into the command line.
916 * Only for non-special registers.
917 * Used by CTRL-R command in command-line mode
918 * insert_reg() can't be used here, because special characters from the
919 * register contents will be interpreted as commands.
920 *
921 * return FAIL for failure, OK otherwise
922 */
923 int
924cmdline_paste_reg(
925 int regname,
926 int literally_arg, // Insert text literally instead of "as typed"
927 int remcr) // don't add CR characters
928{
929 long i;
930 int literally = literally_arg;
931
932 if (get_yank_register(regname, FALSE))
933 literally = TRUE;
934 if (y_current->y_array == NULL)
935 return FAIL;
936
937 for (i = 0; i < y_current->y_size; ++i)
938 {
939 cmdline_paste_str(y_current->y_array[i], literally);
940
941 // Insert ^M between lines and after last line if type is MLINE.
942 // Don't do this when "remcr" is TRUE.
943 if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
944 cmdline_paste_str((char_u *)"\r", literally);
945
946 // Check for CTRL-C, in case someone tries to paste a few thousand
947 // lines and gets bored.
948 ui_breakcheck();
949 if (got_int)
950 return FAIL;
951 }
952 return OK;
953}
954
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200955/*
956 * Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
957 */
958 void
959shift_delete_registers()
960{
961 int n;
962
963 y_current = &y_regs[9];
964 free_yank_all(); // free register nine
965 for (n = 9; n > 1; --n)
966 y_regs[n] = y_regs[n - 1];
967 y_current = &y_regs[1];
968 if (!y_append)
969 y_previous = y_current;
970 y_regs[1].y_array = NULL; // set register one to empty
971}
972
973#if defined(FEAT_EVAL)
974 void
975yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
976{
977 static int recursive = FALSE;
978 dict_T *v_event;
979 list_T *list;
980 int n;
981 char_u buf[NUMBUFLEN + 2];
982 long reglen = 0;
983
984 if (recursive)
985 return;
986
987 v_event = get_vim_var_dict(VV_EVENT);
988
989 list = list_alloc();
990 if (list == NULL)
991 return;
992 for (n = 0; n < reg->y_size; n++)
993 list_append_string(list, reg->y_array[n], -1);
994 list->lv_lock = VAR_FIXED;
Bram Moolenaar6d90c612020-06-29 20:23:32 +0200995 (void)dict_add_list(v_event, "regcontents", list);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +0200996
997 buf[0] = (char_u)oap->regname;
998 buf[1] = NUL;
Bram Moolenaar6d90c612020-06-29 20:23:32 +0200999 (void)dict_add_string(v_event, "regname", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001000
1001 buf[0] = get_op_char(oap->op_type);
1002 buf[1] = get_extra_op_char(oap->op_type);
1003 buf[2] = NUL;
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001004 (void)dict_add_string(v_event, "operator", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001005
1006 buf[0] = NUL;
1007 buf[1] = NUL;
1008 switch (get_reg_type(oap->regname, &reglen))
1009 {
1010 case MLINE: buf[0] = 'V'; break;
1011 case MCHAR: buf[0] = 'v'; break;
1012 case MBLOCK:
1013 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
1014 reglen + 1);
1015 break;
1016 }
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001017 (void)dict_add_string(v_event, "regtype", buf);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001018
Bram Moolenaar6d90c612020-06-29 20:23:32 +02001019 (void)dict_add_bool(v_event, "visual", oap->is_VIsual);
Bram Moolenaar37d16732020-06-12 22:09:01 +02001020
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001021 // Lock the dictionary and its keys
1022 dict_set_items_ro(v_event);
1023
1024 recursive = TRUE;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001025 textwinlock++;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001026 apply_autocmds(EVENT_TEXTYANKPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02001027 textwinlock--;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001028 recursive = FALSE;
1029
1030 // Empty the dictionary, v:event is still valid
1031 dict_free_contents(v_event);
1032 hash_init(&v_event->dv_hashtab);
1033}
1034#endif
1035
1036/*
1037 * set all the yank registers to empty (called from main())
1038 */
1039 void
1040init_yank(void)
1041{
1042 int i;
1043
1044 for (i = 0; i < NUM_REGISTERS; ++i)
1045 y_regs[i].y_array = NULL;
1046}
1047
1048#if defined(EXITFREE) || defined(PROTO)
1049 void
1050clear_registers(void)
1051{
1052 int i;
1053
1054 for (i = 0; i < NUM_REGISTERS; ++i)
1055 {
1056 y_current = &y_regs[i];
1057 if (y_current->y_array != NULL)
1058 free_yank_all();
1059 }
1060}
1061#endif
1062
1063/*
1064 * Free "n" lines from the current yank register.
1065 * Called for normal freeing and in case of error.
1066 */
1067 static void
1068free_yank(long n)
1069{
1070 if (y_current->y_array != NULL)
1071 {
1072 long i;
1073
1074 for (i = n; --i >= 0; )
1075 {
1076#ifdef AMIGA // only for very slow machines
1077 if ((i & 1023) == 1023) // this may take a while
1078 {
1079 // This message should never cause a hit-return message.
1080 // Overwrite this message with any next message.
1081 ++no_wait_return;
1082 smsg(_("freeing %ld lines"), i + 1);
1083 --no_wait_return;
1084 msg_didout = FALSE;
1085 msg_col = 0;
1086 }
1087#endif
1088 vim_free(y_current->y_array[i]);
1089 }
1090 VIM_CLEAR(y_current->y_array);
1091#ifdef AMIGA
1092 if (n >= 1000)
1093 msg("");
1094#endif
1095 }
1096}
1097
Bram Moolenaar45fffdf2020-03-24 21:42:01 +01001098 void
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001099free_yank_all(void)
1100{
1101 free_yank(y_current->y_size);
1102}
1103
1104/*
1105 * Yank the text between "oap->start" and "oap->end" into a yank register.
1106 * If we are to append (uppercase register), we first yank into a new yank
1107 * register and then concatenate the old and the new one (so we keep the old
1108 * one in case of out-of-memory).
1109 *
1110 * Return FAIL for failure, OK otherwise.
1111 */
1112 int
1113op_yank(oparg_T *oap, int deleting, int mess)
1114{
1115 long y_idx; // index in y_array[]
1116 yankreg_T *curr; // copy of y_current
1117 yankreg_T newreg; // new yank register when appending
1118 char_u **new_ptr;
1119 linenr_T lnum; // current line number
1120 long j;
1121 int yanktype = oap->motion_type;
1122 long yanklines = oap->line_count;
1123 linenr_T yankendlnum = oap->end.lnum;
1124 char_u *p;
1125 char_u *pnew;
1126 struct block_def bd;
1127#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
1128 int did_star = FALSE;
1129#endif
1130
1131 // check for read-only register
1132 if (oap->regname != 0 && !valid_yank_reg(oap->regname, TRUE))
1133 {
1134 beep_flush();
1135 return FAIL;
1136 }
1137 if (oap->regname == '_') // black hole: nothing to do
1138 return OK;
1139
1140#ifdef FEAT_CLIPBOARD
1141 if (!clip_star.available && oap->regname == '*')
1142 oap->regname = 0;
1143 else if (!clip_plus.available && oap->regname == '+')
1144 oap->regname = 0;
1145#endif
1146
1147 if (!deleting) // op_delete() already set y_current
1148 get_yank_register(oap->regname, TRUE);
1149
1150 curr = y_current;
1151 // append to existing contents
1152 if (y_append && y_current->y_array != NULL)
1153 y_current = &newreg;
1154 else
1155 free_yank_all(); // free previously yanked lines
1156
1157 // If the cursor was in column 1 before and after the movement, and the
1158 // operator is not inclusive, the yank is always linewise.
1159 if ( oap->motion_type == MCHAR
1160 && oap->start.col == 0
1161 && !oap->inclusive
1162 && (!oap->is_VIsual || *p_sel == 'o')
1163 && !oap->block_mode
1164 && oap->end.col == 0
1165 && yanklines > 1)
1166 {
1167 yanktype = MLINE;
1168 --yankendlnum;
1169 --yanklines;
1170 }
1171
1172 y_current->y_size = yanklines;
1173 y_current->y_type = yanktype; // set the yank register type
1174 y_current->y_width = 0;
1175 y_current->y_array = lalloc_clear(sizeof(char_u *) * yanklines, TRUE);
1176 if (y_current->y_array == NULL)
1177 {
1178 y_current = curr;
1179 return FAIL;
1180 }
1181#ifdef FEAT_VIMINFO
1182 y_current->y_time_set = vim_time();
1183#endif
1184
1185 y_idx = 0;
1186 lnum = oap->start.lnum;
1187
1188 if (oap->block_mode)
1189 {
1190 // Visual block mode
1191 y_current->y_type = MBLOCK; // set the yank register type
1192 y_current->y_width = oap->end_vcol - oap->start_vcol;
1193
1194 if (curwin->w_curswant == MAXCOL && y_current->y_width > 0)
1195 y_current->y_width--;
1196 }
1197
1198 for ( ; lnum <= yankendlnum; lnum++, y_idx++)
1199 {
1200 switch (y_current->y_type)
1201 {
1202 case MBLOCK:
1203 block_prep(oap, &bd, lnum, FALSE);
1204 if (yank_copy_line(&bd, y_idx) == FAIL)
1205 goto fail;
1206 break;
1207
1208 case MLINE:
1209 if ((y_current->y_array[y_idx] =
1210 vim_strsave(ml_get(lnum))) == NULL)
1211 goto fail;
1212 break;
1213
1214 case MCHAR:
1215 {
1216 colnr_T startcol = 0, endcol = MAXCOL;
1217 int is_oneChar = FALSE;
1218 colnr_T cs, ce;
1219
1220 p = ml_get(lnum);
1221 bd.startspaces = 0;
1222 bd.endspaces = 0;
1223
1224 if (lnum == oap->start.lnum)
1225 {
1226 startcol = oap->start.col;
1227 if (virtual_op)
1228 {
1229 getvcol(curwin, &oap->start, &cs, NULL, &ce);
1230 if (ce != cs && oap->start.coladd > 0)
1231 {
1232 // Part of a tab selected -- but don't
1233 // double-count it.
1234 bd.startspaces = (ce - cs + 1)
1235 - oap->start.coladd;
1236 startcol++;
1237 }
1238 }
1239 }
1240
1241 if (lnum == oap->end.lnum)
1242 {
1243 endcol = oap->end.col;
1244 if (virtual_op)
1245 {
1246 getvcol(curwin, &oap->end, &cs, NULL, &ce);
1247 if (p[endcol] == NUL || (cs + oap->end.coladd < ce
1248 // Don't add space for double-wide
1249 // char; endcol will be on last byte
1250 // of multi-byte char.
1251 && (*mb_head_off)(p, p + endcol) == 0))
1252 {
1253 if (oap->start.lnum == oap->end.lnum
1254 && oap->start.col == oap->end.col)
1255 {
1256 // Special case: inside a single char
1257 is_oneChar = TRUE;
1258 bd.startspaces = oap->end.coladd
1259 - oap->start.coladd + oap->inclusive;
1260 endcol = startcol;
1261 }
1262 else
1263 {
1264 bd.endspaces = oap->end.coladd
1265 + oap->inclusive;
1266 endcol -= oap->inclusive;
1267 }
1268 }
1269 }
1270 }
1271 if (endcol == MAXCOL)
1272 endcol = (colnr_T)STRLEN(p);
1273 if (startcol > endcol || is_oneChar)
1274 bd.textlen = 0;
1275 else
1276 bd.textlen = endcol - startcol + oap->inclusive;
1277 bd.textstart = p + startcol;
1278 if (yank_copy_line(&bd, y_idx) == FAIL)
1279 goto fail;
1280 break;
1281 }
1282 // NOTREACHED
1283 }
1284 }
1285
1286 if (curr != y_current) // append the new block to the old block
1287 {
1288 new_ptr = ALLOC_MULT(char_u *, curr->y_size + y_current->y_size);
1289 if (new_ptr == NULL)
1290 goto fail;
1291 for (j = 0; j < curr->y_size; ++j)
1292 new_ptr[j] = curr->y_array[j];
1293 vim_free(curr->y_array);
1294 curr->y_array = new_ptr;
1295#ifdef FEAT_VIMINFO
1296 curr->y_time_set = vim_time();
1297#endif
1298
1299 if (yanktype == MLINE) // MLINE overrides MCHAR and MBLOCK
1300 curr->y_type = MLINE;
1301
1302 // Concatenate the last line of the old block with the first line of
1303 // the new block, unless being Vi compatible.
1304 if (curr->y_type == MCHAR && vim_strchr(p_cpo, CPO_REGAPPEND) == NULL)
1305 {
1306 pnew = alloc(STRLEN(curr->y_array[curr->y_size - 1])
1307 + STRLEN(y_current->y_array[0]) + 1);
1308 if (pnew == NULL)
1309 {
1310 y_idx = y_current->y_size - 1;
1311 goto fail;
1312 }
1313 STRCPY(pnew, curr->y_array[--j]);
1314 STRCAT(pnew, y_current->y_array[0]);
1315 vim_free(curr->y_array[j]);
1316 vim_free(y_current->y_array[0]);
1317 curr->y_array[j++] = pnew;
1318 y_idx = 1;
1319 }
1320 else
1321 y_idx = 0;
1322 while (y_idx < y_current->y_size)
1323 curr->y_array[j++] = y_current->y_array[y_idx++];
1324 curr->y_size = j;
1325 vim_free(y_current->y_array);
1326 y_current = curr;
1327 }
1328 if (curwin->w_p_rnu)
1329 redraw_later(SOME_VALID); // cursor moved to start
1330 if (mess) // Display message about yank?
1331 {
1332 if (yanktype == MCHAR
1333 && !oap->block_mode
1334 && yanklines == 1)
1335 yanklines = 0;
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001336 // Some versions of Vi use ">=" here, some don't...
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001337 if (yanklines > p_report)
1338 {
1339 char namebuf[100];
1340
1341 if (oap->regname == NUL)
1342 *namebuf = NUL;
1343 else
1344 vim_snprintf(namebuf, sizeof(namebuf),
1345 _(" into \"%c"), oap->regname);
1346
1347 // redisplay now, so message is not deleted
1348 update_topline_redraw();
1349 if (oap->block_mode)
1350 {
1351 smsg(NGETTEXT("block of %ld line yanked%s",
1352 "block of %ld lines yanked%s", yanklines),
1353 yanklines, namebuf);
1354 }
1355 else
1356 {
1357 smsg(NGETTEXT("%ld line yanked%s",
1358 "%ld lines yanked%s", yanklines),
1359 yanklines, namebuf);
1360 }
1361 }
1362 }
1363
Bram Moolenaare1004402020-10-24 20:49:43 +02001364 if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001365 {
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001366 // Set "'[" and "']" marks.
1367 curbuf->b_op_start = oap->start;
1368 curbuf->b_op_end = oap->end;
1369 if (yanktype == MLINE && !oap->block_mode)
1370 {
1371 curbuf->b_op_start.col = 0;
1372 curbuf->b_op_end.col = MAXCOL;
1373 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001374 }
1375
1376#ifdef FEAT_CLIPBOARD
1377 // If we were yanking to the '*' register, send result to clipboard.
1378 // If no register was specified, and "unnamed" in 'clipboard', make a copy
1379 // to the '*' register.
1380 if (clip_star.available
1381 && (curr == &(y_regs[STAR_REGISTER])
1382 || (!deleting && oap->regname == 0
1383 && ((clip_unnamed | clip_unnamed_saved) & CLIP_UNNAMED))))
1384 {
1385 if (curr != &(y_regs[STAR_REGISTER]))
1386 // Copy the text from register 0 to the clipboard register.
1387 copy_yank_reg(&(y_regs[STAR_REGISTER]));
1388
1389 clip_own_selection(&clip_star);
1390 clip_gen_set_selection(&clip_star);
1391# ifdef FEAT_X11
1392 did_star = TRUE;
1393# endif
1394 }
1395
1396# ifdef FEAT_X11
1397 // If we were yanking to the '+' register, send result to selection.
1398 // Also copy to the '*' register, in case auto-select is off.
1399 if (clip_plus.available
1400 && (curr == &(y_regs[PLUS_REGISTER])
1401 || (!deleting && oap->regname == 0
1402 && ((clip_unnamed | clip_unnamed_saved) &
1403 CLIP_UNNAMED_PLUS))))
1404 {
1405 if (curr != &(y_regs[PLUS_REGISTER]))
1406 // Copy the text from register 0 to the clipboard register.
1407 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
1408
1409 clip_own_selection(&clip_plus);
1410 clip_gen_set_selection(&clip_plus);
1411 if (!clip_isautosel_star() && !clip_isautosel_plus()
1412 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
1413 {
1414 copy_yank_reg(&(y_regs[STAR_REGISTER]));
1415 clip_own_selection(&clip_star);
1416 clip_gen_set_selection(&clip_star);
1417 }
1418 }
1419# endif
1420#endif
1421
1422#if defined(FEAT_EVAL)
1423 if (!deleting && has_textyankpost())
1424 yank_do_autocmd(oap, y_current);
1425#endif
1426
1427 return OK;
1428
1429fail: // free the allocated lines
1430 free_yank(y_idx + 1);
1431 y_current = curr;
1432 return FAIL;
1433}
1434
1435 static int
1436yank_copy_line(struct block_def *bd, long y_idx)
1437{
1438 char_u *pnew;
1439
1440 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
1441 == NULL)
1442 return FAIL;
1443 y_current->y_array[y_idx] = pnew;
1444 vim_memset(pnew, ' ', (size_t)bd->startspaces);
1445 pnew += bd->startspaces;
1446 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
1447 pnew += bd->textlen;
1448 vim_memset(pnew, ' ', (size_t)bd->endspaces);
1449 pnew += bd->endspaces;
1450 *pnew = NUL;
1451 return OK;
1452}
1453
1454#ifdef FEAT_CLIPBOARD
1455/*
1456 * Make a copy of the y_current register to register "reg".
1457 */
1458 static void
1459copy_yank_reg(yankreg_T *reg)
1460{
1461 yankreg_T *curr = y_current;
1462 long j;
1463
1464 y_current = reg;
1465 free_yank_all();
1466 *y_current = *curr;
1467 y_current->y_array = lalloc_clear(
1468 sizeof(char_u *) * y_current->y_size, TRUE);
1469 if (y_current->y_array == NULL)
1470 y_current->y_size = 0;
1471 else
1472 for (j = 0; j < y_current->y_size; ++j)
1473 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
1474 {
1475 free_yank(j);
1476 y_current->y_size = 0;
1477 break;
1478 }
1479 y_current = curr;
1480}
1481#endif
1482
1483/*
1484 * Put contents of register "regname" into the text.
1485 * Caller must check "regname" to be valid!
1486 * "flags": PUT_FIXINDENT make indent look nice
1487 * PUT_CURSEND leave cursor after end of new text
1488 * PUT_LINE force linewise put (":put")
1489 */
1490 void
1491do_put(
1492 int regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001493 char_u *expr_result, // result for regname "=" when compiled
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001494 int dir, // BACKWARD for 'P', FORWARD for 'p'
1495 long count,
1496 int flags)
1497{
1498 char_u *ptr;
1499 char_u *newp, *oldp;
1500 int yanklen;
1501 int totlen = 0; // init for gcc
1502 linenr_T lnum;
1503 colnr_T col;
1504 long i; // index in y_array[]
1505 int y_type;
1506 long y_size;
1507 int oldlen;
1508 long y_width = 0;
1509 colnr_T vcol;
1510 int delcount;
1511 int incr = 0;
1512 long j;
1513 struct block_def bd;
1514 char_u **y_array = NULL;
1515 long nr_lines = 0;
1516 pos_T new_cursor;
1517 int indent;
1518 int orig_indent = 0; // init for gcc
1519 int indent_diff = 0; // init for gcc
1520 int first_indent = TRUE;
1521 int lendiff = 0;
1522 pos_T old_pos;
1523 char_u *insert_string = NULL;
1524 int allocated = FALSE;
1525 long cnt;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001526 pos_T orig_start = curbuf->b_op_start;
1527 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001528
1529#ifdef FEAT_CLIPBOARD
1530 // Adjust register name for "unnamed" in 'clipboard'.
1531 adjust_clip_reg(&regname);
1532 (void)may_get_selection(regname);
1533#endif
1534
1535 if (flags & PUT_FIXINDENT)
1536 orig_indent = get_indent();
1537
1538 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1539 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1540
1541 // Using inserted text works differently, because the register includes
1542 // special characters (newlines, etc.).
1543 if (regname == '.')
1544 {
1545 if (VIsual_active)
1546 stuffcharReadbuff(VIsual_mode);
1547 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
1548 (count == -1 ? 'O' : 'i')), count, FALSE);
1549 // Putting the text is done later, so can't really move the cursor to
1550 // the next character. Use "l" to simulate it.
1551 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
1552 stuffcharReadbuff('l');
1553 return;
1554 }
1555
1556 // For special registers '%' (file name), '#' (alternate file name) and
1557 // ':' (last command line), etc. we have to create a fake yank register.
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001558 // For compiled code "expr_result" holds the expression result.
1559 if (regname == '=' && expr_result != NULL)
1560 insert_string = expr_result;
1561 else if (get_spec_reg(regname, &insert_string, &allocated, TRUE)
1562 && insert_string == NULL)
1563 return;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001564
1565 // Autocommands may be executed when saving lines for undo. This might
1566 // make "y_array" invalid, so we start undo now to avoid that.
1567 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
1568 goto end;
1569
1570 if (insert_string != NULL)
1571 {
1572 y_type = MCHAR;
1573#ifdef FEAT_EVAL
1574 if (regname == '=')
1575 {
1576 // For the = register we need to split the string at NL
1577 // characters.
1578 // Loop twice: count the number of lines and save them.
1579 for (;;)
1580 {
1581 y_size = 0;
1582 ptr = insert_string;
1583 while (ptr != NULL)
1584 {
1585 if (y_array != NULL)
1586 y_array[y_size] = ptr;
1587 ++y_size;
1588 ptr = vim_strchr(ptr, '\n');
1589 if (ptr != NULL)
1590 {
1591 if (y_array != NULL)
1592 *ptr = NUL;
1593 ++ptr;
1594 // A trailing '\n' makes the register linewise.
1595 if (*ptr == NUL)
1596 {
1597 y_type = MLINE;
1598 break;
1599 }
1600 }
1601 }
1602 if (y_array != NULL)
1603 break;
1604 y_array = ALLOC_MULT(char_u *, y_size);
1605 if (y_array == NULL)
1606 goto end;
1607 }
1608 }
1609 else
1610#endif
1611 {
1612 y_size = 1; // use fake one-line yank register
1613 y_array = &insert_string;
1614 }
1615 }
1616 else
1617 {
1618 get_yank_register(regname, FALSE);
1619
1620 y_type = y_current->y_type;
1621 y_width = y_current->y_width;
1622 y_size = y_current->y_size;
1623 y_array = y_current->y_array;
1624 }
1625
1626 if (y_type == MLINE)
1627 {
1628 if (flags & PUT_LINE_SPLIT)
1629 {
1630 char_u *p;
1631
1632 // "p" or "P" in Visual mode: split the lines to put the text in
1633 // between.
1634 if (u_save_cursor() == FAIL)
1635 goto end;
1636 p = ml_get_cursor();
1637 if (dir == FORWARD && *p != NUL)
1638 MB_PTR_ADV(p);
1639 ptr = vim_strsave(p);
1640 if (ptr == NULL)
1641 goto end;
1642 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
1643 vim_free(ptr);
1644
1645 oldp = ml_get_curline();
1646 p = oldp + curwin->w_cursor.col;
1647 if (dir == FORWARD && *p != NUL)
1648 MB_PTR_ADV(p);
1649 ptr = vim_strnsave(oldp, p - oldp);
1650 if (ptr == NULL)
1651 goto end;
1652 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
1653 ++nr_lines;
1654 dir = FORWARD;
1655 }
1656 if (flags & PUT_LINE_FORWARD)
1657 {
1658 // Must be "p" for a Visual block, put lines below the block.
1659 curwin->w_cursor = curbuf->b_visual.vi_end;
1660 dir = FORWARD;
1661 }
1662 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1663 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1664 }
1665
1666 if (flags & PUT_LINE) // :put command or "p" in Visual line mode.
1667 y_type = MLINE;
1668
1669 if (y_size == 0 || y_array == NULL)
1670 {
1671 semsg(_("E353: Nothing in register %s"),
1672 regname == 0 ? (char_u *)"\"" : transchar(regname));
1673 goto end;
1674 }
1675
1676 if (y_type == MBLOCK)
1677 {
1678 lnum = curwin->w_cursor.lnum + y_size + 1;
1679 if (lnum > curbuf->b_ml.ml_line_count)
1680 lnum = curbuf->b_ml.ml_line_count + 1;
1681 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
1682 goto end;
1683 }
1684 else if (y_type == MLINE)
1685 {
1686 lnum = curwin->w_cursor.lnum;
1687#ifdef FEAT_FOLDING
1688 // Correct line number for closed fold. Don't move the cursor yet,
1689 // u_save() uses it.
1690 if (dir == BACKWARD)
1691 (void)hasFolding(lnum, &lnum, NULL);
1692 else
1693 (void)hasFolding(lnum, NULL, &lnum);
1694#endif
1695 if (dir == FORWARD)
1696 ++lnum;
1697 // In an empty buffer the empty line is going to be replaced, include
1698 // it in the saved lines.
1699 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
1700 goto end;
1701#ifdef FEAT_FOLDING
1702 if (dir == FORWARD)
1703 curwin->w_cursor.lnum = lnum - 1;
1704 else
1705 curwin->w_cursor.lnum = lnum;
1706 curbuf->b_op_start = curwin->w_cursor; // for mark_adjust()
1707#endif
1708 }
1709 else if (u_save_cursor() == FAIL)
1710 goto end;
1711
1712 yanklen = (int)STRLEN(y_array[0]);
1713
1714 if (ve_flags == VE_ALL && y_type == MCHAR)
1715 {
1716 if (gchar_cursor() == TAB)
1717 {
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001718 int viscol = getviscol();
1719 int ts = curbuf->b_p_ts;
1720
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001721 // Don't need to insert spaces when "p" on the last position of a
1722 // tab or "P" on the first position.
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001723 if (dir == FORWARD ?
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001724#ifdef FEAT_VARTABS
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001725 tabstop_padding(viscol, ts, curbuf->b_p_vts_array) != 1
1726#else
1727 ts - (viscol % ts) != 1
1728#endif
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001729 : curwin->w_cursor.coladd > 0)
1730 coladvance_force(viscol);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001731 else
1732 curwin->w_cursor.coladd = 0;
1733 }
1734 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
1735 coladvance_force(getviscol() + (dir == FORWARD));
1736 }
1737
1738 lnum = curwin->w_cursor.lnum;
1739 col = curwin->w_cursor.col;
1740
1741 // Block mode
1742 if (y_type == MBLOCK)
1743 {
1744 int c = gchar_cursor();
1745 colnr_T endcol2 = 0;
1746
1747 if (dir == FORWARD && c != NUL)
1748 {
1749 if (ve_flags == VE_ALL)
1750 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1751 else
1752 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
1753
1754 if (has_mbyte)
1755 // move to start of next multi-byte character
1756 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
1757 else
1758 if (c != TAB || ve_flags != VE_ALL)
1759 ++curwin->w_cursor.col;
1760 ++col;
1761 }
1762 else
1763 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1764
1765 col += curwin->w_cursor.coladd;
1766 if (ve_flags == VE_ALL
1767 && (curwin->w_cursor.coladd > 0
1768 || endcol2 == curwin->w_cursor.col))
1769 {
1770 if (dir == FORWARD && c == NUL)
1771 ++col;
Bram Moolenaaref85a9b2020-07-10 20:24:07 +02001772 if (dir != FORWARD && c != NUL && curwin->w_cursor.coladd > 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001773 ++curwin->w_cursor.col;
1774 if (c == TAB)
1775 {
1776 if (dir == BACKWARD && curwin->w_cursor.col)
1777 curwin->w_cursor.col--;
1778 if (dir == FORWARD && col - 1 == endcol2)
1779 curwin->w_cursor.col++;
1780 }
1781 }
1782 curwin->w_cursor.coladd = 0;
1783 bd.textcol = 0;
1784 for (i = 0; i < y_size; ++i)
1785 {
1786 int spaces;
1787 char shortline;
1788
1789 bd.startspaces = 0;
1790 bd.endspaces = 0;
1791 vcol = 0;
1792 delcount = 0;
1793
1794 // add a new line
1795 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1796 {
1797 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
1798 (colnr_T)1, FALSE) == FAIL)
1799 break;
1800 ++nr_lines;
1801 }
1802 // get the old line and advance to the position to insert at
1803 oldp = ml_get_curline();
1804 oldlen = (int)STRLEN(oldp);
1805 for (ptr = oldp; vcol < col && *ptr; )
1806 {
1807 // Count a tab for what it's worth (if list mode not on)
1808 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
1809 vcol += incr;
1810 }
1811 bd.textcol = (colnr_T)(ptr - oldp);
1812
1813 shortline = (vcol < col) || (vcol == col && !*ptr) ;
1814
1815 if (vcol < col) // line too short, padd with spaces
1816 bd.startspaces = col - vcol;
1817 else if (vcol > col)
1818 {
1819 bd.endspaces = vcol - col;
1820 bd.startspaces = incr - bd.endspaces;
1821 --bd.textcol;
1822 delcount = 1;
1823 if (has_mbyte)
1824 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
1825 if (oldp[bd.textcol] != TAB)
1826 {
1827 // Only a Tab can be split into spaces. Other
1828 // characters will have to be moved to after the
1829 // block, causing misalignment.
1830 delcount = 0;
1831 bd.endspaces = 0;
1832 }
1833 }
1834
1835 yanklen = (int)STRLEN(y_array[i]);
1836
1837 // calculate number of spaces required to fill right side of block
1838 spaces = y_width + 1;
1839 for (j = 0; j < yanklen; j++)
1840 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
1841 if (spaces < 0)
1842 spaces = 0;
1843
1844 // insert the new text
1845 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
1846 newp = alloc(totlen + oldlen + 1);
1847 if (newp == NULL)
1848 break;
1849 // copy part up to cursor to new line
1850 ptr = newp;
1851 mch_memmove(ptr, oldp, (size_t)bd.textcol);
1852 ptr += bd.textcol;
1853 // may insert some spaces before the new text
1854 vim_memset(ptr, ' ', (size_t)bd.startspaces);
1855 ptr += bd.startspaces;
1856 // insert the new text
1857 for (j = 0; j < count; ++j)
1858 {
1859 mch_memmove(ptr, y_array[i], (size_t)yanklen);
1860 ptr += yanklen;
1861
1862 // insert block's trailing spaces only if there's text behind
1863 if ((j < count - 1 || !shortline) && spaces)
1864 {
1865 vim_memset(ptr, ' ', (size_t)spaces);
1866 ptr += spaces;
1867 }
1868 }
1869 // may insert some spaces after the new text
1870 vim_memset(ptr, ' ', (size_t)bd.endspaces);
1871 ptr += bd.endspaces;
1872 // move the text after the cursor to the end of the line.
1873 mch_memmove(ptr, oldp + bd.textcol + delcount,
1874 (size_t)(oldlen - bd.textcol - delcount + 1));
1875 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
1876
1877 ++curwin->w_cursor.lnum;
1878 if (i == 0)
1879 curwin->w_cursor.col += bd.startspaces;
1880 }
1881
1882 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
1883
1884 // Set '[ mark.
1885 curbuf->b_op_start = curwin->w_cursor;
1886 curbuf->b_op_start.lnum = lnum;
1887
1888 // adjust '] mark
1889 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
1890 curbuf->b_op_end.col = bd.textcol + totlen - 1;
1891 curbuf->b_op_end.coladd = 0;
1892 if (flags & PUT_CURSEND)
1893 {
1894 colnr_T len;
1895
1896 curwin->w_cursor = curbuf->b_op_end;
1897 curwin->w_cursor.col++;
1898
1899 // in Insert mode we might be after the NUL, correct for that
1900 len = (colnr_T)STRLEN(ml_get_curline());
1901 if (curwin->w_cursor.col > len)
1902 curwin->w_cursor.col = len;
1903 }
1904 else
1905 curwin->w_cursor.lnum = lnum;
1906 }
1907 else
1908 {
1909 // Character or Line mode
1910 if (y_type == MCHAR)
1911 {
1912 // if type is MCHAR, FORWARD is the same as BACKWARD on the next
1913 // char
1914 if (dir == FORWARD && gchar_cursor() != NUL)
1915 {
1916 if (has_mbyte)
1917 {
1918 int bytelen = (*mb_ptr2len)(ml_get_cursor());
1919
1920 // put it on the next of the multi-byte character.
1921 col += bytelen;
1922 if (yanklen)
1923 {
1924 curwin->w_cursor.col += bytelen;
1925 curbuf->b_op_end.col += bytelen;
1926 }
1927 }
1928 else
1929 {
1930 ++col;
1931 if (yanklen)
1932 {
1933 ++curwin->w_cursor.col;
1934 ++curbuf->b_op_end.col;
1935 }
1936 }
1937 }
1938 curbuf->b_op_start = curwin->w_cursor;
1939 }
1940 // Line mode: BACKWARD is the same as FORWARD on the previous line
1941 else if (dir == BACKWARD)
1942 --lnum;
1943 new_cursor = curwin->w_cursor;
1944
Bram Moolenaarcd942772020-08-22 21:08:44 +02001945 // simple case: insert into one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001946 if (y_type == MCHAR && y_size == 1)
1947 {
Bram Moolenaarcd942772020-08-22 21:08:44 +02001948 linenr_T end_lnum = 0; // init for gcc
1949 linenr_T start_lnum = lnum;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001950
1951 if (VIsual_active)
1952 {
1953 end_lnum = curbuf->b_visual.vi_end.lnum;
1954 if (end_lnum < curbuf->b_visual.vi_start.lnum)
1955 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaarcd942772020-08-22 21:08:44 +02001956 if (end_lnum > start_lnum)
1957 {
1958 pos_T pos;
1959
1960 // "col" is valid for the first line, in following lines
1961 // the virtual column needs to be used. Matters for
1962 // multi-byte characters.
1963 pos.lnum = lnum;
1964 pos.col = col;
1965 pos.coladd = 0;
1966 getvcol(curwin, &pos, NULL, &vcol, NULL);
1967 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001968 }
1969
1970 do {
1971 totlen = count * yanklen;
1972 if (totlen > 0)
1973 {
1974 oldp = ml_get(lnum);
Bram Moolenaarcd942772020-08-22 21:08:44 +02001975 if (lnum > start_lnum)
1976 {
1977 pos_T pos;
1978
1979 pos.lnum = lnum;
1980 if (getvpos(&pos, vcol) == OK)
1981 col = pos.col;
1982 else
1983 col = MAXCOL;
1984 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001985 if (VIsual_active && col > (int)STRLEN(oldp))
1986 {
1987 lnum++;
1988 continue;
1989 }
1990 newp = alloc(STRLEN(oldp) + totlen + 1);
1991 if (newp == NULL)
1992 goto end; // alloc() gave an error message
1993 mch_memmove(newp, oldp, (size_t)col);
1994 ptr = newp + col;
1995 for (i = 0; i < count; ++i)
1996 {
1997 mch_memmove(ptr, y_array[0], (size_t)yanklen);
1998 ptr += yanklen;
1999 }
2000 STRMOVE(ptr, oldp + col);
2001 ml_replace(lnum, newp, FALSE);
2002 // Place cursor on last putted char.
2003 if (lnum == curwin->w_cursor.lnum)
2004 {
2005 // make sure curwin->w_virtcol is updated
2006 changed_cline_bef_curs();
2007 curwin->w_cursor.col += (colnr_T)(totlen - 1);
2008 }
2009 }
2010 if (VIsual_active)
2011 lnum++;
2012 } while (VIsual_active && lnum <= end_lnum);
2013
2014 if (VIsual_active) // reset lnum to the last visual line
2015 lnum--;
2016
2017 curbuf->b_op_end = curwin->w_cursor;
2018 // For "CTRL-O p" in Insert mode, put cursor after last char
2019 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
2020 ++curwin->w_cursor.col;
2021 changed_bytes(lnum, col);
2022 }
2023 else
2024 {
2025 // Insert at least one line. When y_type is MCHAR, break the first
2026 // line in two.
2027 for (cnt = 1; cnt <= count; ++cnt)
2028 {
2029 i = 0;
2030 if (y_type == MCHAR)
2031 {
2032 // Split the current line in two at the insert position.
2033 // First insert y_array[size - 1] in front of second line.
2034 // Then append y_array[0] to first line.
2035 lnum = new_cursor.lnum;
2036 ptr = ml_get(lnum) + col;
2037 totlen = (int)STRLEN(y_array[y_size - 1]);
2038 newp = alloc(STRLEN(ptr) + totlen + 1);
2039 if (newp == NULL)
2040 goto error;
2041 STRCPY(newp, y_array[y_size - 1]);
2042 STRCAT(newp, ptr);
2043 // insert second line
2044 ml_append(lnum, newp, (colnr_T)0, FALSE);
2045 vim_free(newp);
2046
2047 oldp = ml_get(lnum);
2048 newp = alloc(col + yanklen + 1);
2049 if (newp == NULL)
2050 goto error;
2051 // copy first part of line
2052 mch_memmove(newp, oldp, (size_t)col);
2053 // append to first line
2054 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
2055 ml_replace(lnum, newp, FALSE);
2056
2057 curwin->w_cursor.lnum = lnum;
2058 i = 1;
2059 }
2060
2061 for (; i < y_size; ++i)
2062 {
2063 if ((y_type != MCHAR || i < y_size - 1)
2064 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
2065 == FAIL)
2066 goto error;
2067 lnum++;
2068 ++nr_lines;
2069 if (flags & PUT_FIXINDENT)
2070 {
2071 old_pos = curwin->w_cursor;
2072 curwin->w_cursor.lnum = lnum;
2073 ptr = ml_get(lnum);
2074 if (cnt == count && i == y_size - 1)
2075 lendiff = (int)STRLEN(ptr);
2076#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
2077 if (*ptr == '#' && preprocs_left())
2078 indent = 0; // Leave # lines at start
2079 else
2080#endif
2081 if (*ptr == NUL)
2082 indent = 0; // Ignore empty lines
2083 else if (first_indent)
2084 {
2085 indent_diff = orig_indent - get_indent();
2086 indent = orig_indent;
2087 first_indent = FALSE;
2088 }
2089 else if ((indent = get_indent() + indent_diff) < 0)
2090 indent = 0;
2091 (void)set_indent(indent, 0);
2092 curwin->w_cursor = old_pos;
2093 // remember how many chars were removed
2094 if (cnt == count && i == y_size - 1)
2095 lendiff -= (int)STRLEN(ml_get(lnum));
2096 }
2097 }
2098 }
2099
2100error:
2101 // Adjust marks.
2102 if (y_type == MLINE)
2103 {
2104 curbuf->b_op_start.col = 0;
2105 if (dir == FORWARD)
2106 curbuf->b_op_start.lnum++;
2107 }
2108 // Skip mark_adjust when adding lines after the last one, there
2109 // can't be marks there. But still needed in diff mode.
2110 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
2111 < curbuf->b_ml.ml_line_count
2112#ifdef FEAT_DIFF
2113 || curwin->w_p_diff
2114#endif
2115 )
2116 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
2117 (linenr_T)MAXLNUM, nr_lines, 0L);
2118
2119 // note changed text for displaying and folding
2120 if (y_type == MCHAR)
2121 changed_lines(curwin->w_cursor.lnum, col,
2122 curwin->w_cursor.lnum + 1, nr_lines);
2123 else
2124 changed_lines(curbuf->b_op_start.lnum, 0,
2125 curbuf->b_op_start.lnum, nr_lines);
2126
2127 // put '] mark at last inserted character
2128 curbuf->b_op_end.lnum = lnum;
2129 // correct length for change in indent
2130 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
2131 if (col > 1)
2132 curbuf->b_op_end.col = col - 1;
2133 else
2134 curbuf->b_op_end.col = 0;
2135
2136 if (flags & PUT_CURSLINE)
2137 {
2138 // ":put": put cursor on last inserted line
2139 curwin->w_cursor.lnum = lnum;
2140 beginline(BL_WHITE | BL_FIX);
2141 }
2142 else if (flags & PUT_CURSEND)
2143 {
2144 // put cursor after inserted text
2145 if (y_type == MLINE)
2146 {
2147 if (lnum >= curbuf->b_ml.ml_line_count)
2148 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2149 else
2150 curwin->w_cursor.lnum = lnum + 1;
2151 curwin->w_cursor.col = 0;
2152 }
2153 else
2154 {
2155 curwin->w_cursor.lnum = lnum;
2156 curwin->w_cursor.col = col;
2157 }
2158 }
2159 else if (y_type == MLINE)
2160 {
2161 // put cursor on first non-blank in first inserted line
2162 curwin->w_cursor.col = 0;
2163 if (dir == FORWARD)
2164 ++curwin->w_cursor.lnum;
2165 beginline(BL_WHITE | BL_FIX);
2166 }
2167 else // put cursor on first inserted character
2168 curwin->w_cursor = new_cursor;
2169 }
2170 }
2171
2172 msgmore(nr_lines);
2173 curwin->w_set_curswant = TRUE;
2174
2175end:
Bram Moolenaare1004402020-10-24 20:49:43 +02002176 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002177 {
2178 curbuf->b_op_start = orig_start;
2179 curbuf->b_op_end = orig_end;
2180 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002181 if (allocated)
2182 vim_free(insert_string);
2183 if (regname == '=')
2184 vim_free(y_array);
2185
2186 VIsual_active = FALSE;
2187
2188 // If the cursor is past the end of the line put it at the end.
2189 adjust_cursor_eol();
2190}
2191
2192/*
2193 * Return the character name of the register with the given number.
2194 */
2195 int
2196get_register_name(int num)
2197{
2198 if (num == -1)
2199 return '"';
2200 else if (num < 10)
2201 return num + '0';
2202 else if (num == DELETION_REGISTER)
2203 return '-';
2204#ifdef FEAT_CLIPBOARD
2205 else if (num == STAR_REGISTER)
2206 return '*';
2207 else if (num == PLUS_REGISTER)
2208 return '+';
2209#endif
2210 else
2211 {
2212#ifdef EBCDIC
2213 int i;
2214
2215 // EBCDIC is really braindead ...
2216 i = 'a' + (num - 10);
2217 if (i > 'i')
2218 i += 7;
2219 if (i > 'r')
2220 i += 8;
2221 return i;
2222#else
2223 return num + 'a' - 10;
2224#endif
2225 }
2226}
2227
2228/*
Bram Moolenaarbb861e22020-06-07 18:16:36 +02002229 * Return the index of the register "" points to.
2230 */
2231 int
2232get_unname_register()
2233{
2234 return y_previous == NULL ? -1 : y_previous - &y_regs[0];
2235}
2236
2237/*
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002238 * ":dis" and ":registers": Display the contents of the yank registers.
2239 */
2240 void
2241ex_display(exarg_T *eap)
2242{
2243 int i, n;
2244 long j;
2245 char_u *p;
2246 yankreg_T *yb;
2247 int name;
2248 int attr;
2249 char_u *arg = eap->arg;
2250 int clen;
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002251 int type;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002252
2253 if (arg != NULL && *arg == NUL)
2254 arg = NULL;
2255 attr = HL_ATTR(HLF_8);
2256
2257 // Highlight title
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002258 msg_puts_title(_("\nType Name Content"));
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002259 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
2260 {
2261 name = get_register_name(i);
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002262 switch (get_reg_type(name, NULL))
2263 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002264 case MLINE: type = 'l'; break;
2265 case MCHAR: type = 'c'; break;
2266 default: type = 'b'; break;
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002267 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002268 if (arg != NULL && vim_strchr(arg, name) == NULL
2269#ifdef ONE_CLIPBOARD
2270 // Star register and plus register contain the same thing.
2271 && (name != '*' || vim_strchr(arg, '+') == NULL)
2272#endif
2273 )
2274 continue; // did not ask for this register
2275
2276#ifdef FEAT_CLIPBOARD
2277 // Adjust register name for "unnamed" in 'clipboard'.
2278 // When it's a clipboard register, fill it with the current contents
2279 // of the clipboard.
2280 adjust_clip_reg(&name);
2281 (void)may_get_selection(name);
2282#endif
2283
2284 if (i == -1)
2285 {
2286 if (y_previous != NULL)
2287 yb = y_previous;
2288 else
2289 yb = &(y_regs[0]);
2290 }
2291 else
2292 yb = &(y_regs[i]);
2293
2294#ifdef FEAT_EVAL
2295 if (name == MB_TOLOWER(redir_reg)
2296 || (redir_reg == '"' && yb == y_previous))
2297 continue; // do not list register being written to, the
2298 // pointer can be freed
2299#endif
2300
2301 if (yb->y_array != NULL)
2302 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002303 int do_show = FALSE;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002304
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002305 for (j = 0; !do_show && j < yb->y_size; ++j)
2306 do_show = !message_filtered(yb->y_array[j]);
2307
2308 if (do_show || yb->y_size == 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002309 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002310 msg_putchar('\n');
2311 msg_puts(" ");
2312 msg_putchar(type);
2313 msg_puts(" ");
2314 msg_putchar('"');
2315 msg_putchar(name);
2316 msg_puts(" ");
2317
2318 n = (int)Columns - 11;
2319 for (j = 0; j < yb->y_size && n > 1; ++j)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002320 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002321 if (j)
2322 {
2323 msg_puts_attr("^J", attr);
2324 n -= 2;
2325 }
2326 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0;
2327 ++p)
2328 {
2329 clen = (*mb_ptr2len)(p);
2330 msg_outtrans_len(p, clen);
2331 p += clen - 1;
2332 }
2333 }
2334 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002335 msg_puts_attr("^J", attr);
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002336 out_flush(); // show one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002337 }
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002338 ui_breakcheck();
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002339 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002340 }
2341
2342 // display last inserted text
2343 if ((p = get_last_insert()) != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002344 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int
2345 && !message_filtered(p))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002346 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002347 msg_puts("\n c \". ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002348 dis_msg(p, TRUE);
2349 }
2350
2351 // display last command line
2352 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002353 && !got_int && !message_filtered(last_cmdline))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002354 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002355 msg_puts("\n c \": ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002356 dis_msg(last_cmdline, FALSE);
2357 }
2358
2359 // display current file name
2360 if (curbuf->b_fname != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002361 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int
2362 && !message_filtered(curbuf->b_fname))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002363 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002364 msg_puts("\n c \"% ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002365 dis_msg(curbuf->b_fname, FALSE);
2366 }
2367
2368 // display alternate file name
2369 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
2370 {
2371 char_u *fname;
2372 linenr_T dummy;
2373
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002374 if (buflist_name_nr(0, &fname, &dummy) != FAIL
2375 && !message_filtered(fname))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002376 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002377 msg_puts("\n c \"# ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002378 dis_msg(fname, FALSE);
2379 }
2380 }
2381
2382 // display last search pattern
2383 if (last_search_pat() != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002384 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int
2385 && !message_filtered(last_search_pat()))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002386 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002387 msg_puts("\n c \"/ ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002388 dis_msg(last_search_pat(), FALSE);
2389 }
2390
2391#ifdef FEAT_EVAL
2392 // display last used expression
2393 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002394 && !got_int && !message_filtered(expr_line))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002395 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002396 msg_puts("\n c \"= ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002397 dis_msg(expr_line, FALSE);
2398 }
2399#endif
2400}
2401
2402/*
2403 * display a string for do_dis()
2404 * truncate at end of screen line
2405 */
2406 static void
2407dis_msg(
2408 char_u *p,
2409 int skip_esc) // if TRUE, ignore trailing ESC
2410{
2411 int n;
2412 int l;
2413
2414 n = (int)Columns - 6;
2415 while (*p != NUL
2416 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
2417 && (n -= ptr2cells(p)) >= 0)
2418 {
2419 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
2420 {
2421 msg_outtrans_len(p, l);
2422 p += l;
2423 }
2424 else
2425 msg_outtrans_len(p++, 1);
2426 }
2427 ui_breakcheck();
2428}
2429
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002430#if defined(FEAT_DND) || defined(PROTO)
2431/*
2432 * Replace the contents of the '~' register with str.
2433 */
2434 void
2435dnd_yank_drag_data(char_u *str, long len)
2436{
2437 yankreg_T *curr;
2438
2439 curr = y_current;
2440 y_current = &y_regs[TILDE_REGISTER];
2441 free_yank_all();
2442 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
2443 y_current = curr;
2444}
2445#endif
2446
2447
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002448/*
2449 * Return the type of a register.
2450 * Used for getregtype()
2451 * Returns MAUTO for error.
2452 */
2453 char_u
2454get_reg_type(int regname, long *reglen)
2455{
2456 switch (regname)
2457 {
2458 case '%': // file name
2459 case '#': // alternate file name
2460 case '=': // expression
2461 case ':': // last command line
2462 case '/': // last search-pattern
2463 case '.': // last inserted text
2464# ifdef FEAT_SEARCHPATH
2465 case Ctrl_F: // Filename under cursor
2466 case Ctrl_P: // Path under cursor, expand via "path"
2467# endif
2468 case Ctrl_W: // word under cursor
2469 case Ctrl_A: // WORD (mnemonic All) under cursor
2470 case '_': // black hole: always empty
2471 return MCHAR;
2472 }
2473
2474# ifdef FEAT_CLIPBOARD
2475 regname = may_get_selection(regname);
2476# endif
2477
2478 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2479 return MAUTO;
2480
2481 get_yank_register(regname, FALSE);
2482
2483 if (y_current->y_array != NULL)
2484 {
2485 if (reglen != NULL && y_current->y_type == MBLOCK)
2486 *reglen = y_current->y_width;
2487 return y_current->y_type;
2488 }
2489 return MAUTO;
2490}
2491
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002492#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002493/*
2494 * When "flags" has GREG_LIST return a list with text "s".
2495 * Otherwise just return "s".
2496 */
2497 static char_u *
2498getreg_wrap_one_line(char_u *s, int flags)
2499{
2500 if (flags & GREG_LIST)
2501 {
2502 list_T *list = list_alloc();
2503
2504 if (list != NULL)
2505 {
2506 if (list_append_string(list, NULL, -1) == FAIL)
2507 {
2508 list_free(list);
2509 return NULL;
2510 }
2511 list->lv_first->li_tv.vval.v_string = s;
2512 }
2513 return (char_u *)list;
2514 }
2515 return s;
2516}
2517
2518/*
Bram Moolenaard672dde2020-02-26 13:43:51 +01002519 * Return the contents of a register as a single allocated string or as a list.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002520 * Used for "@r" in expressions and for getreg().
2521 * Returns NULL for error.
2522 * Flags:
2523 * GREG_NO_EXPR Do not allow expression register
2524 * GREG_EXPR_SRC For the expression register: return expression itself,
2525 * not the result of its evaluation.
Bram Moolenaard672dde2020-02-26 13:43:51 +01002526 * GREG_LIST Return a list of lines instead of a single string.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002527 */
2528 char_u *
2529get_reg_contents(int regname, int flags)
2530{
2531 long i;
2532 char_u *retval;
2533 int allocated;
2534 long len;
2535
2536 // Don't allow using an expression register inside an expression
2537 if (regname == '=')
2538 {
2539 if (flags & GREG_NO_EXPR)
2540 return NULL;
2541 if (flags & GREG_EXPR_SRC)
2542 return getreg_wrap_one_line(get_expr_line_src(), flags);
2543 return getreg_wrap_one_line(get_expr_line(), flags);
2544 }
2545
2546 if (regname == '@') // "@@" is used for unnamed register
2547 regname = '"';
2548
2549 // check for valid regname
2550 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2551 return NULL;
2552
2553# ifdef FEAT_CLIPBOARD
2554 regname = may_get_selection(regname);
2555# endif
2556
2557 if (get_spec_reg(regname, &retval, &allocated, FALSE))
2558 {
2559 if (retval == NULL)
2560 return NULL;
2561 if (allocated)
2562 return getreg_wrap_one_line(retval, flags);
2563 return getreg_wrap_one_line(vim_strsave(retval), flags);
2564 }
2565
2566 get_yank_register(regname, FALSE);
2567 if (y_current->y_array == NULL)
2568 return NULL;
2569
2570 if (flags & GREG_LIST)
2571 {
2572 list_T *list = list_alloc();
2573 int error = FALSE;
2574
2575 if (list == NULL)
2576 return NULL;
2577 for (i = 0; i < y_current->y_size; ++i)
2578 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
2579 error = TRUE;
2580 if (error)
2581 {
2582 list_free(list);
2583 return NULL;
2584 }
2585 return (char_u *)list;
2586 }
2587
2588 // Compute length of resulting string.
2589 len = 0;
2590 for (i = 0; i < y_current->y_size; ++i)
2591 {
2592 len += (long)STRLEN(y_current->y_array[i]);
2593 // Insert a newline between lines and after last line if
2594 // y_type is MLINE.
2595 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2596 ++len;
2597 }
2598
2599 retval = alloc(len + 1);
2600
2601 // Copy the lines of the yank register into the string.
2602 if (retval != NULL)
2603 {
2604 len = 0;
2605 for (i = 0; i < y_current->y_size; ++i)
2606 {
2607 STRCPY(retval + len, y_current->y_array[i]);
2608 len += (long)STRLEN(retval + len);
2609
2610 // Insert a NL between lines and after the last line if y_type is
2611 // MLINE.
2612 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2613 retval[len++] = '\n';
2614 }
2615 retval[len] = NUL;
2616 }
2617
2618 return retval;
2619}
2620
2621 static int
2622init_write_reg(
2623 int name,
2624 yankreg_T **old_y_previous,
2625 yankreg_T **old_y_current,
2626 int must_append,
2627 int *yank_type UNUSED)
2628{
2629 if (!valid_yank_reg(name, TRUE)) // check for valid reg name
2630 {
2631 emsg_invreg(name);
2632 return FAIL;
2633 }
2634
2635 // Don't want to change the current (unnamed) register
2636 *old_y_previous = y_previous;
2637 *old_y_current = y_current;
2638
2639 get_yank_register(name, TRUE);
2640 if (!y_append && !must_append)
2641 free_yank_all();
2642 return OK;
2643}
2644
2645 static void
2646finish_write_reg(
2647 int name,
2648 yankreg_T *old_y_previous,
2649 yankreg_T *old_y_current)
2650{
2651# ifdef FEAT_CLIPBOARD
2652 // Send text of clipboard register to the clipboard.
2653 may_set_selection();
2654# endif
2655
2656 // ':let @" = "val"' should change the meaning of the "" register
2657 if (name != '"')
2658 y_previous = old_y_previous;
2659 y_current = old_y_current;
2660}
2661
2662/*
2663 * Store string "str" in register "name".
2664 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
2665 * If "must_append" is TRUE, always append to the register. Otherwise append
2666 * if "name" is an uppercase letter.
2667 * Note: "maxlen" and "must_append" don't work for the "/" register.
2668 * Careful: 'str' is modified, you may have to use a copy!
2669 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
2670 */
2671 void
2672write_reg_contents(
2673 int name,
2674 char_u *str,
2675 int maxlen,
2676 int must_append)
2677{
2678 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
2679}
2680
2681 void
2682write_reg_contents_lst(
2683 int name,
2684 char_u **strings,
2685 int maxlen UNUSED,
2686 int must_append,
2687 int yank_type,
2688 long block_len)
2689{
2690 yankreg_T *old_y_previous, *old_y_current;
2691
2692 if (name == '/' || name == '=')
2693 {
2694 char_u *s;
2695
2696 if (strings[0] == NULL)
2697 s = (char_u *)"";
2698 else if (strings[1] != NULL)
2699 {
2700 emsg(_("E883: search pattern and expression register may not "
2701 "contain two or more lines"));
2702 return;
2703 }
2704 else
2705 s = strings[0];
2706 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
2707 return;
2708 }
2709
2710 if (name == '_') // black hole: nothing to do
2711 return;
2712
2713 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2714 &yank_type) == FAIL)
2715 return;
2716
2717 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
2718
2719 finish_write_reg(name, old_y_previous, old_y_current);
2720}
2721
2722 void
2723write_reg_contents_ex(
2724 int name,
2725 char_u *str,
2726 int maxlen,
2727 int must_append,
2728 int yank_type,
2729 long block_len)
2730{
2731 yankreg_T *old_y_previous, *old_y_current;
2732 long len;
2733
2734 if (maxlen >= 0)
2735 len = maxlen;
2736 else
2737 len = (long)STRLEN(str);
2738
2739 // Special case: '/' search pattern
2740 if (name == '/')
2741 {
2742 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
2743 return;
2744 }
2745
2746 if (name == '#')
2747 {
2748 buf_T *buf;
2749
2750 if (VIM_ISDIGIT(*str))
2751 {
2752 int num = atoi((char *)str);
2753
2754 buf = buflist_findnr(num);
2755 if (buf == NULL)
2756 semsg(_(e_nobufnr), (long)num);
2757 }
2758 else
2759 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
2760 TRUE, FALSE, FALSE));
2761 if (buf == NULL)
2762 return;
2763 curwin->w_alt_fnum = buf->b_fnum;
2764 return;
2765 }
2766
2767 if (name == '=')
2768 {
2769 char_u *p, *s;
2770
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002771 p = vim_strnsave(str, len);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002772 if (p == NULL)
2773 return;
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002774 if (must_append && expr_line != NULL)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002775 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002776 s = concat_str(expr_line, p);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002777 vim_free(p);
2778 p = s;
2779 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002780 set_expr_line(p, NULL);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002781 return;
2782 }
2783
2784 if (name == '_') // black hole: nothing to do
2785 return;
2786
2787 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2788 &yank_type) == FAIL)
2789 return;
2790
2791 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
2792
2793 finish_write_reg(name, old_y_previous, old_y_current);
2794}
2795#endif // FEAT_EVAL
2796
2797#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
2798/*
2799 * Put a string into a register. When the register is not empty, the string
2800 * is appended.
2801 */
Bram Moolenaar45fffdf2020-03-24 21:42:01 +01002802 void
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002803str_to_reg(
2804 yankreg_T *y_ptr, // pointer to yank register
2805 int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO
2806 char_u *str, // string to put in register
2807 long len, // length of string
2808 long blocklen, // width of Visual block
2809 int str_list) // TRUE if str is char_u **
2810{
2811 int type; // MCHAR, MLINE or MBLOCK
2812 int lnum;
2813 long start;
2814 long i;
2815 int extra;
2816 int newlines; // number of lines added
2817 int extraline = 0; // extra line at the end
2818 int append = FALSE; // append to last line in register
2819 char_u *s;
2820 char_u **ss;
2821 char_u **pp;
2822 long maxlen;
2823
2824 if (y_ptr->y_array == NULL) // NULL means empty register
2825 y_ptr->y_size = 0;
2826
2827 if (yank_type == MAUTO)
2828 type = ((str_list || (len > 0 && (str[len - 1] == NL
2829 || str[len - 1] == CAR)))
2830 ? MLINE : MCHAR);
2831 else
2832 type = yank_type;
2833
2834 // Count the number of lines within the string
2835 newlines = 0;
2836 if (str_list)
2837 {
2838 for (ss = (char_u **) str; *ss != NULL; ++ss)
2839 ++newlines;
2840 }
2841 else
2842 {
2843 for (i = 0; i < len; i++)
2844 if (str[i] == '\n')
2845 ++newlines;
2846 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
2847 {
2848 extraline = 1;
2849 ++newlines; // count extra newline at the end
2850 }
2851 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
2852 {
2853 append = TRUE;
2854 --newlines; // uncount newline when appending first line
2855 }
2856 }
2857
2858 // Without any lines make the register empty.
2859 if (y_ptr->y_size + newlines == 0)
2860 {
2861 VIM_CLEAR(y_ptr->y_array);
2862 return;
2863 }
2864
2865 // Allocate an array to hold the pointers to the new register lines.
2866 // If the register was not empty, move the existing lines to the new array.
2867 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
2868 if (pp == NULL) // out of memory
2869 return;
2870 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
2871 pp[lnum] = y_ptr->y_array[lnum];
2872 vim_free(y_ptr->y_array);
2873 y_ptr->y_array = pp;
2874 maxlen = 0;
2875
2876 // Find the end of each line and save it into the array.
2877 if (str_list)
2878 {
2879 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
2880 {
2881 i = (long)STRLEN(*ss);
2882 pp[lnum] = vim_strnsave(*ss, i);
2883 if (i > maxlen)
2884 maxlen = i;
2885 }
2886 }
2887 else
2888 {
2889 for (start = 0; start < len + extraline; start += i + 1)
2890 {
2891 for (i = start; i < len; ++i) // find the end of the line
2892 if (str[i] == '\n')
2893 break;
2894 i -= start; // i is now length of line
2895 if (i > maxlen)
2896 maxlen = i;
2897 if (append)
2898 {
2899 --lnum;
2900 extra = (int)STRLEN(y_ptr->y_array[lnum]);
2901 }
2902 else
2903 extra = 0;
2904 s = alloc(i + extra + 1);
2905 if (s == NULL)
2906 break;
2907 if (extra)
2908 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
2909 if (append)
2910 vim_free(y_ptr->y_array[lnum]);
2911 if (i)
2912 mch_memmove(s + extra, str + start, (size_t)i);
2913 extra += i;
2914 s[extra] = NUL;
2915 y_ptr->y_array[lnum++] = s;
2916 while (--extra >= 0)
2917 {
2918 if (*s == NUL)
2919 *s = '\n'; // replace NUL with newline
2920 ++s;
2921 }
2922 append = FALSE; // only first line is appended
2923 }
2924 }
2925 y_ptr->y_type = type;
2926 y_ptr->y_size = lnum;
2927 if (type == MBLOCK)
2928 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
2929 else
2930 y_ptr->y_width = 0;
2931# ifdef FEAT_VIMINFO
2932 y_ptr->y_time_set = vim_time();
2933# endif
2934}
2935#endif // FEAT_CLIPBOARD || FEAT_EVAL || PROTO