blob: 56b6cadecd9856af26cb541b16568c52f7b62ba0 [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.
1405 // Also copy to the '*' register, in case auto-select is off.
1406 if (clip_plus.available
1407 && (curr == &(y_regs[PLUS_REGISTER])
1408 || (!deleting && oap->regname == 0
1409 && ((clip_unnamed | clip_unnamed_saved) &
1410 CLIP_UNNAMED_PLUS))))
1411 {
1412 if (curr != &(y_regs[PLUS_REGISTER]))
1413 // Copy the text from register 0 to the clipboard register.
1414 copy_yank_reg(&(y_regs[PLUS_REGISTER]));
1415
1416 clip_own_selection(&clip_plus);
1417 clip_gen_set_selection(&clip_plus);
1418 if (!clip_isautosel_star() && !clip_isautosel_plus()
1419 && !did_star && curr == &(y_regs[PLUS_REGISTER]))
1420 {
1421 copy_yank_reg(&(y_regs[STAR_REGISTER]));
1422 clip_own_selection(&clip_star);
1423 clip_gen_set_selection(&clip_star);
1424 }
1425 }
1426# endif
1427#endif
1428
1429#if defined(FEAT_EVAL)
1430 if (!deleting && has_textyankpost())
1431 yank_do_autocmd(oap, y_current);
1432#endif
1433
1434 return OK;
1435
1436fail: // free the allocated lines
1437 free_yank(y_idx + 1);
1438 y_current = curr;
1439 return FAIL;
1440}
1441
1442 static int
1443yank_copy_line(struct block_def *bd, long y_idx)
1444{
1445 char_u *pnew;
1446
1447 if ((pnew = alloc(bd->startspaces + bd->endspaces + bd->textlen + 1))
1448 == NULL)
1449 return FAIL;
1450 y_current->y_array[y_idx] = pnew;
1451 vim_memset(pnew, ' ', (size_t)bd->startspaces);
1452 pnew += bd->startspaces;
1453 mch_memmove(pnew, bd->textstart, (size_t)bd->textlen);
1454 pnew += bd->textlen;
1455 vim_memset(pnew, ' ', (size_t)bd->endspaces);
1456 pnew += bd->endspaces;
1457 *pnew = NUL;
1458 return OK;
1459}
1460
1461#ifdef FEAT_CLIPBOARD
1462/*
1463 * Make a copy of the y_current register to register "reg".
1464 */
1465 static void
1466copy_yank_reg(yankreg_T *reg)
1467{
1468 yankreg_T *curr = y_current;
1469 long j;
1470
1471 y_current = reg;
1472 free_yank_all();
1473 *y_current = *curr;
1474 y_current->y_array = lalloc_clear(
1475 sizeof(char_u *) * y_current->y_size, TRUE);
1476 if (y_current->y_array == NULL)
1477 y_current->y_size = 0;
1478 else
1479 for (j = 0; j < y_current->y_size; ++j)
1480 if ((y_current->y_array[j] = vim_strsave(curr->y_array[j])) == NULL)
1481 {
1482 free_yank(j);
1483 y_current->y_size = 0;
1484 break;
1485 }
1486 y_current = curr;
1487}
1488#endif
1489
1490/*
1491 * Put contents of register "regname" into the text.
1492 * Caller must check "regname" to be valid!
1493 * "flags": PUT_FIXINDENT make indent look nice
1494 * PUT_CURSEND leave cursor after end of new text
1495 * PUT_LINE force linewise put (":put")
1496 */
1497 void
1498do_put(
1499 int regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001500 char_u *expr_result, // result for regname "=" when compiled
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001501 int dir, // BACKWARD for 'P', FORWARD for 'p'
1502 long count,
1503 int flags)
1504{
1505 char_u *ptr;
1506 char_u *newp, *oldp;
1507 int yanklen;
1508 int totlen = 0; // init for gcc
1509 linenr_T lnum;
1510 colnr_T col;
1511 long i; // index in y_array[]
1512 int y_type;
1513 long y_size;
1514 int oldlen;
1515 long y_width = 0;
1516 colnr_T vcol;
1517 int delcount;
1518 int incr = 0;
1519 long j;
1520 struct block_def bd;
1521 char_u **y_array = NULL;
1522 long nr_lines = 0;
1523 pos_T new_cursor;
1524 int indent;
1525 int orig_indent = 0; // init for gcc
1526 int indent_diff = 0; // init for gcc
1527 int first_indent = TRUE;
1528 int lendiff = 0;
1529 pos_T old_pos;
1530 char_u *insert_string = NULL;
1531 int allocated = FALSE;
1532 long cnt;
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001533 pos_T orig_start = curbuf->b_op_start;
1534 pos_T orig_end = curbuf->b_op_end;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001535
1536#ifdef FEAT_CLIPBOARD
1537 // Adjust register name for "unnamed" in 'clipboard'.
1538 adjust_clip_reg(&regname);
1539 (void)may_get_selection(regname);
1540#endif
1541
1542 if (flags & PUT_FIXINDENT)
1543 orig_indent = get_indent();
1544
1545 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1546 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1547
1548 // Using inserted text works differently, because the register includes
1549 // special characters (newlines, etc.).
1550 if (regname == '.')
1551 {
1552 if (VIsual_active)
1553 stuffcharReadbuff(VIsual_mode);
1554 (void)stuff_inserted((dir == FORWARD ? (count == -1 ? 'o' : 'a') :
1555 (count == -1 ? 'O' : 'i')), count, FALSE);
1556 // Putting the text is done later, so can't really move the cursor to
1557 // the next character. Use "l" to simulate it.
1558 if ((flags & PUT_CURSEND) && gchar_cursor() != NUL)
1559 stuffcharReadbuff('l');
1560 return;
1561 }
1562
1563 // For special registers '%' (file name), '#' (alternate file name) and
1564 // ':' (last command line), etc. we have to create a fake yank register.
Bram Moolenaarc3516f72020-09-08 22:45:35 +02001565 // For compiled code "expr_result" holds the expression result.
1566 if (regname == '=' && expr_result != NULL)
1567 insert_string = expr_result;
1568 else if (get_spec_reg(regname, &insert_string, &allocated, TRUE)
1569 && insert_string == NULL)
1570 return;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001571
1572 // Autocommands may be executed when saving lines for undo. This might
1573 // make "y_array" invalid, so we start undo now to avoid that.
1574 if (u_save(curwin->w_cursor.lnum, curwin->w_cursor.lnum + 1) == FAIL)
1575 goto end;
1576
1577 if (insert_string != NULL)
1578 {
1579 y_type = MCHAR;
1580#ifdef FEAT_EVAL
1581 if (regname == '=')
1582 {
1583 // For the = register we need to split the string at NL
1584 // characters.
1585 // Loop twice: count the number of lines and save them.
1586 for (;;)
1587 {
1588 y_size = 0;
1589 ptr = insert_string;
1590 while (ptr != NULL)
1591 {
1592 if (y_array != NULL)
1593 y_array[y_size] = ptr;
1594 ++y_size;
1595 ptr = vim_strchr(ptr, '\n');
1596 if (ptr != NULL)
1597 {
1598 if (y_array != NULL)
1599 *ptr = NUL;
1600 ++ptr;
1601 // A trailing '\n' makes the register linewise.
1602 if (*ptr == NUL)
1603 {
1604 y_type = MLINE;
1605 break;
1606 }
1607 }
1608 }
1609 if (y_array != NULL)
1610 break;
1611 y_array = ALLOC_MULT(char_u *, y_size);
1612 if (y_array == NULL)
1613 goto end;
1614 }
1615 }
1616 else
1617#endif
1618 {
1619 y_size = 1; // use fake one-line yank register
1620 y_array = &insert_string;
1621 }
1622 }
1623 else
1624 {
1625 get_yank_register(regname, FALSE);
1626
1627 y_type = y_current->y_type;
1628 y_width = y_current->y_width;
1629 y_size = y_current->y_size;
1630 y_array = y_current->y_array;
1631 }
1632
1633 if (y_type == MLINE)
1634 {
1635 if (flags & PUT_LINE_SPLIT)
1636 {
1637 char_u *p;
1638
1639 // "p" or "P" in Visual mode: split the lines to put the text in
1640 // between.
1641 if (u_save_cursor() == FAIL)
1642 goto end;
1643 p = ml_get_cursor();
1644 if (dir == FORWARD && *p != NUL)
1645 MB_PTR_ADV(p);
1646 ptr = vim_strsave(p);
1647 if (ptr == NULL)
1648 goto end;
1649 ml_append(curwin->w_cursor.lnum, ptr, (colnr_T)0, FALSE);
1650 vim_free(ptr);
1651
1652 oldp = ml_get_curline();
1653 p = oldp + curwin->w_cursor.col;
1654 if (dir == FORWARD && *p != NUL)
1655 MB_PTR_ADV(p);
1656 ptr = vim_strnsave(oldp, p - oldp);
1657 if (ptr == NULL)
1658 goto end;
1659 ml_replace(curwin->w_cursor.lnum, ptr, FALSE);
1660 ++nr_lines;
1661 dir = FORWARD;
1662 }
1663 if (flags & PUT_LINE_FORWARD)
1664 {
1665 // Must be "p" for a Visual block, put lines below the block.
1666 curwin->w_cursor = curbuf->b_visual.vi_end;
1667 dir = FORWARD;
1668 }
1669 curbuf->b_op_start = curwin->w_cursor; // default for '[ mark
1670 curbuf->b_op_end = curwin->w_cursor; // default for '] mark
1671 }
1672
1673 if (flags & PUT_LINE) // :put command or "p" in Visual line mode.
1674 y_type = MLINE;
1675
1676 if (y_size == 0 || y_array == NULL)
1677 {
1678 semsg(_("E353: Nothing in register %s"),
1679 regname == 0 ? (char_u *)"\"" : transchar(regname));
1680 goto end;
1681 }
1682
1683 if (y_type == MBLOCK)
1684 {
1685 lnum = curwin->w_cursor.lnum + y_size + 1;
1686 if (lnum > curbuf->b_ml.ml_line_count)
1687 lnum = curbuf->b_ml.ml_line_count + 1;
1688 if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL)
1689 goto end;
1690 }
1691 else if (y_type == MLINE)
1692 {
1693 lnum = curwin->w_cursor.lnum;
1694#ifdef FEAT_FOLDING
1695 // Correct line number for closed fold. Don't move the cursor yet,
1696 // u_save() uses it.
1697 if (dir == BACKWARD)
1698 (void)hasFolding(lnum, &lnum, NULL);
1699 else
1700 (void)hasFolding(lnum, NULL, &lnum);
1701#endif
1702 if (dir == FORWARD)
1703 ++lnum;
1704 // In an empty buffer the empty line is going to be replaced, include
1705 // it in the saved lines.
1706 if ((BUFEMPTY() ? u_save(0, 2) : u_save(lnum - 1, lnum)) == FAIL)
1707 goto end;
1708#ifdef FEAT_FOLDING
1709 if (dir == FORWARD)
1710 curwin->w_cursor.lnum = lnum - 1;
1711 else
1712 curwin->w_cursor.lnum = lnum;
1713 curbuf->b_op_start = curwin->w_cursor; // for mark_adjust()
1714#endif
1715 }
1716 else if (u_save_cursor() == FAIL)
1717 goto end;
1718
1719 yanklen = (int)STRLEN(y_array[0]);
1720
1721 if (ve_flags == VE_ALL && y_type == MCHAR)
1722 {
1723 if (gchar_cursor() == TAB)
1724 {
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001725 int viscol = getviscol();
1726 int ts = curbuf->b_p_ts;
1727
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001728 // Don't need to insert spaces when "p" on the last position of a
1729 // tab or "P" on the first position.
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001730 if (dir == FORWARD ?
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001731#ifdef FEAT_VARTABS
Bram Moolenaar6f1f0ca2019-12-01 18:16:18 +01001732 tabstop_padding(viscol, ts, curbuf->b_p_vts_array) != 1
1733#else
1734 ts - (viscol % ts) != 1
1735#endif
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001736 : curwin->w_cursor.coladd > 0)
1737 coladvance_force(viscol);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001738 else
1739 curwin->w_cursor.coladd = 0;
1740 }
1741 else if (curwin->w_cursor.coladd > 0 || gchar_cursor() == NUL)
1742 coladvance_force(getviscol() + (dir == FORWARD));
1743 }
1744
1745 lnum = curwin->w_cursor.lnum;
1746 col = curwin->w_cursor.col;
1747
1748 // Block mode
1749 if (y_type == MBLOCK)
1750 {
1751 int c = gchar_cursor();
1752 colnr_T endcol2 = 0;
1753
1754 if (dir == FORWARD && c != NUL)
1755 {
1756 if (ve_flags == VE_ALL)
1757 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1758 else
1759 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
1760
1761 if (has_mbyte)
1762 // move to start of next multi-byte character
1763 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
1764 else
1765 if (c != TAB || ve_flags != VE_ALL)
1766 ++curwin->w_cursor.col;
1767 ++col;
1768 }
1769 else
1770 getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2);
1771
1772 col += curwin->w_cursor.coladd;
1773 if (ve_flags == VE_ALL
1774 && (curwin->w_cursor.coladd > 0
1775 || endcol2 == curwin->w_cursor.col))
1776 {
1777 if (dir == FORWARD && c == NUL)
1778 ++col;
Bram Moolenaaref85a9b2020-07-10 20:24:07 +02001779 if (dir != FORWARD && c != NUL && curwin->w_cursor.coladd > 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001780 ++curwin->w_cursor.col;
1781 if (c == TAB)
1782 {
1783 if (dir == BACKWARD && curwin->w_cursor.col)
1784 curwin->w_cursor.col--;
1785 if (dir == FORWARD && col - 1 == endcol2)
1786 curwin->w_cursor.col++;
1787 }
1788 }
1789 curwin->w_cursor.coladd = 0;
1790 bd.textcol = 0;
1791 for (i = 0; i < y_size; ++i)
1792 {
1793 int spaces;
1794 char shortline;
1795
1796 bd.startspaces = 0;
1797 bd.endspaces = 0;
1798 vcol = 0;
1799 delcount = 0;
1800
1801 // add a new line
1802 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1803 {
1804 if (ml_append(curbuf->b_ml.ml_line_count, (char_u *)"",
1805 (colnr_T)1, FALSE) == FAIL)
1806 break;
1807 ++nr_lines;
1808 }
1809 // get the old line and advance to the position to insert at
1810 oldp = ml_get_curline();
1811 oldlen = (int)STRLEN(oldp);
1812 for (ptr = oldp; vcol < col && *ptr; )
1813 {
1814 // Count a tab for what it's worth (if list mode not on)
1815 incr = lbr_chartabsize_adv(oldp, &ptr, (colnr_T)vcol);
1816 vcol += incr;
1817 }
1818 bd.textcol = (colnr_T)(ptr - oldp);
1819
1820 shortline = (vcol < col) || (vcol == col && !*ptr) ;
1821
1822 if (vcol < col) // line too short, padd with spaces
1823 bd.startspaces = col - vcol;
1824 else if (vcol > col)
1825 {
1826 bd.endspaces = vcol - col;
1827 bd.startspaces = incr - bd.endspaces;
1828 --bd.textcol;
1829 delcount = 1;
1830 if (has_mbyte)
1831 bd.textcol -= (*mb_head_off)(oldp, oldp + bd.textcol);
1832 if (oldp[bd.textcol] != TAB)
1833 {
1834 // Only a Tab can be split into spaces. Other
1835 // characters will have to be moved to after the
1836 // block, causing misalignment.
1837 delcount = 0;
1838 bd.endspaces = 0;
1839 }
1840 }
1841
1842 yanklen = (int)STRLEN(y_array[i]);
1843
1844 // calculate number of spaces required to fill right side of block
1845 spaces = y_width + 1;
1846 for (j = 0; j < yanklen; j++)
1847 spaces -= lbr_chartabsize(NULL, &y_array[i][j], 0);
1848 if (spaces < 0)
1849 spaces = 0;
1850
1851 // insert the new text
1852 totlen = count * (yanklen + spaces) + bd.startspaces + bd.endspaces;
1853 newp = alloc(totlen + oldlen + 1);
1854 if (newp == NULL)
1855 break;
1856 // copy part up to cursor to new line
1857 ptr = newp;
1858 mch_memmove(ptr, oldp, (size_t)bd.textcol);
1859 ptr += bd.textcol;
1860 // may insert some spaces before the new text
1861 vim_memset(ptr, ' ', (size_t)bd.startspaces);
1862 ptr += bd.startspaces;
1863 // insert the new text
1864 for (j = 0; j < count; ++j)
1865 {
1866 mch_memmove(ptr, y_array[i], (size_t)yanklen);
1867 ptr += yanklen;
1868
1869 // insert block's trailing spaces only if there's text behind
1870 if ((j < count - 1 || !shortline) && spaces)
1871 {
1872 vim_memset(ptr, ' ', (size_t)spaces);
1873 ptr += spaces;
1874 }
1875 }
1876 // may insert some spaces after the new text
1877 vim_memset(ptr, ' ', (size_t)bd.endspaces);
1878 ptr += bd.endspaces;
1879 // move the text after the cursor to the end of the line.
1880 mch_memmove(ptr, oldp + bd.textcol + delcount,
1881 (size_t)(oldlen - bd.textcol - delcount + 1));
1882 ml_replace(curwin->w_cursor.lnum, newp, FALSE);
1883
1884 ++curwin->w_cursor.lnum;
1885 if (i == 0)
1886 curwin->w_cursor.col += bd.startspaces;
1887 }
1888
1889 changed_lines(lnum, 0, curwin->w_cursor.lnum, nr_lines);
1890
1891 // Set '[ mark.
1892 curbuf->b_op_start = curwin->w_cursor;
1893 curbuf->b_op_start.lnum = lnum;
1894
1895 // adjust '] mark
1896 curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1;
1897 curbuf->b_op_end.col = bd.textcol + totlen - 1;
1898 curbuf->b_op_end.coladd = 0;
1899 if (flags & PUT_CURSEND)
1900 {
1901 colnr_T len;
1902
1903 curwin->w_cursor = curbuf->b_op_end;
1904 curwin->w_cursor.col++;
1905
1906 // in Insert mode we might be after the NUL, correct for that
1907 len = (colnr_T)STRLEN(ml_get_curline());
1908 if (curwin->w_cursor.col > len)
1909 curwin->w_cursor.col = len;
1910 }
1911 else
1912 curwin->w_cursor.lnum = lnum;
1913 }
1914 else
1915 {
1916 // Character or Line mode
1917 if (y_type == MCHAR)
1918 {
1919 // if type is MCHAR, FORWARD is the same as BACKWARD on the next
1920 // char
1921 if (dir == FORWARD && gchar_cursor() != NUL)
1922 {
1923 if (has_mbyte)
1924 {
1925 int bytelen = (*mb_ptr2len)(ml_get_cursor());
1926
1927 // put it on the next of the multi-byte character.
1928 col += bytelen;
1929 if (yanklen)
1930 {
1931 curwin->w_cursor.col += bytelen;
1932 curbuf->b_op_end.col += bytelen;
1933 }
1934 }
1935 else
1936 {
1937 ++col;
1938 if (yanklen)
1939 {
1940 ++curwin->w_cursor.col;
1941 ++curbuf->b_op_end.col;
1942 }
1943 }
1944 }
1945 curbuf->b_op_start = curwin->w_cursor;
1946 }
1947 // Line mode: BACKWARD is the same as FORWARD on the previous line
1948 else if (dir == BACKWARD)
1949 --lnum;
1950 new_cursor = curwin->w_cursor;
1951
Bram Moolenaarcd942772020-08-22 21:08:44 +02001952 // simple case: insert into one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001953 if (y_type == MCHAR && y_size == 1)
1954 {
Bram Moolenaarcd942772020-08-22 21:08:44 +02001955 linenr_T end_lnum = 0; // init for gcc
1956 linenr_T start_lnum = lnum;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001957
1958 if (VIsual_active)
1959 {
1960 end_lnum = curbuf->b_visual.vi_end.lnum;
1961 if (end_lnum < curbuf->b_visual.vi_start.lnum)
1962 end_lnum = curbuf->b_visual.vi_start.lnum;
Bram Moolenaarcd942772020-08-22 21:08:44 +02001963 if (end_lnum > start_lnum)
1964 {
1965 pos_T pos;
1966
1967 // "col" is valid for the first line, in following lines
1968 // the virtual column needs to be used. Matters for
1969 // multi-byte characters.
1970 pos.lnum = lnum;
1971 pos.col = col;
1972 pos.coladd = 0;
1973 getvcol(curwin, &pos, NULL, &vcol, NULL);
1974 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001975 }
1976
1977 do {
1978 totlen = count * yanklen;
1979 if (totlen > 0)
1980 {
1981 oldp = ml_get(lnum);
Bram Moolenaarcd942772020-08-22 21:08:44 +02001982 if (lnum > start_lnum)
1983 {
1984 pos_T pos;
1985
1986 pos.lnum = lnum;
1987 if (getvpos(&pos, vcol) == OK)
1988 col = pos.col;
1989 else
1990 col = MAXCOL;
1991 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02001992 if (VIsual_active && col > (int)STRLEN(oldp))
1993 {
1994 lnum++;
1995 continue;
1996 }
1997 newp = alloc(STRLEN(oldp) + totlen + 1);
1998 if (newp == NULL)
1999 goto end; // alloc() gave an error message
2000 mch_memmove(newp, oldp, (size_t)col);
2001 ptr = newp + col;
2002 for (i = 0; i < count; ++i)
2003 {
2004 mch_memmove(ptr, y_array[0], (size_t)yanklen);
2005 ptr += yanklen;
2006 }
2007 STRMOVE(ptr, oldp + col);
2008 ml_replace(lnum, newp, FALSE);
2009 // Place cursor on last putted char.
2010 if (lnum == curwin->w_cursor.lnum)
2011 {
2012 // make sure curwin->w_virtcol is updated
2013 changed_cline_bef_curs();
2014 curwin->w_cursor.col += (colnr_T)(totlen - 1);
2015 }
2016 }
2017 if (VIsual_active)
2018 lnum++;
2019 } while (VIsual_active && lnum <= end_lnum);
2020
2021 if (VIsual_active) // reset lnum to the last visual line
2022 lnum--;
2023
2024 curbuf->b_op_end = curwin->w_cursor;
2025 // For "CTRL-O p" in Insert mode, put cursor after last char
2026 if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
2027 ++curwin->w_cursor.col;
2028 changed_bytes(lnum, col);
2029 }
2030 else
2031 {
2032 // Insert at least one line. When y_type is MCHAR, break the first
2033 // line in two.
2034 for (cnt = 1; cnt <= count; ++cnt)
2035 {
2036 i = 0;
2037 if (y_type == MCHAR)
2038 {
2039 // Split the current line in two at the insert position.
2040 // First insert y_array[size - 1] in front of second line.
2041 // Then append y_array[0] to first line.
2042 lnum = new_cursor.lnum;
2043 ptr = ml_get(lnum) + col;
2044 totlen = (int)STRLEN(y_array[y_size - 1]);
2045 newp = alloc(STRLEN(ptr) + totlen + 1);
2046 if (newp == NULL)
2047 goto error;
2048 STRCPY(newp, y_array[y_size - 1]);
2049 STRCAT(newp, ptr);
2050 // insert second line
2051 ml_append(lnum, newp, (colnr_T)0, FALSE);
2052 vim_free(newp);
2053
2054 oldp = ml_get(lnum);
2055 newp = alloc(col + yanklen + 1);
2056 if (newp == NULL)
2057 goto error;
2058 // copy first part of line
2059 mch_memmove(newp, oldp, (size_t)col);
2060 // append to first line
2061 mch_memmove(newp + col, y_array[0], (size_t)(yanklen + 1));
2062 ml_replace(lnum, newp, FALSE);
2063
2064 curwin->w_cursor.lnum = lnum;
2065 i = 1;
2066 }
2067
2068 for (; i < y_size; ++i)
2069 {
2070 if ((y_type != MCHAR || i < y_size - 1)
2071 && ml_append(lnum, y_array[i], (colnr_T)0, FALSE)
2072 == FAIL)
2073 goto error;
2074 lnum++;
2075 ++nr_lines;
2076 if (flags & PUT_FIXINDENT)
2077 {
2078 old_pos = curwin->w_cursor;
2079 curwin->w_cursor.lnum = lnum;
2080 ptr = ml_get(lnum);
2081 if (cnt == count && i == y_size - 1)
2082 lendiff = (int)STRLEN(ptr);
2083#if defined(FEAT_SMARTINDENT) || defined(FEAT_CINDENT)
2084 if (*ptr == '#' && preprocs_left())
2085 indent = 0; // Leave # lines at start
2086 else
2087#endif
2088 if (*ptr == NUL)
2089 indent = 0; // Ignore empty lines
2090 else if (first_indent)
2091 {
2092 indent_diff = orig_indent - get_indent();
2093 indent = orig_indent;
2094 first_indent = FALSE;
2095 }
2096 else if ((indent = get_indent() + indent_diff) < 0)
2097 indent = 0;
2098 (void)set_indent(indent, 0);
2099 curwin->w_cursor = old_pos;
2100 // remember how many chars were removed
2101 if (cnt == count && i == y_size - 1)
2102 lendiff -= (int)STRLEN(ml_get(lnum));
2103 }
2104 }
2105 }
2106
2107error:
2108 // Adjust marks.
2109 if (y_type == MLINE)
2110 {
2111 curbuf->b_op_start.col = 0;
2112 if (dir == FORWARD)
2113 curbuf->b_op_start.lnum++;
2114 }
2115 // Skip mark_adjust when adding lines after the last one, there
2116 // can't be marks there. But still needed in diff mode.
2117 if (curbuf->b_op_start.lnum + (y_type == MCHAR) - 1 + nr_lines
2118 < curbuf->b_ml.ml_line_count
2119#ifdef FEAT_DIFF
2120 || curwin->w_p_diff
2121#endif
2122 )
2123 mark_adjust(curbuf->b_op_start.lnum + (y_type == MCHAR),
2124 (linenr_T)MAXLNUM, nr_lines, 0L);
2125
2126 // note changed text for displaying and folding
2127 if (y_type == MCHAR)
2128 changed_lines(curwin->w_cursor.lnum, col,
2129 curwin->w_cursor.lnum + 1, nr_lines);
2130 else
2131 changed_lines(curbuf->b_op_start.lnum, 0,
2132 curbuf->b_op_start.lnum, nr_lines);
2133
2134 // put '] mark at last inserted character
2135 curbuf->b_op_end.lnum = lnum;
2136 // correct length for change in indent
2137 col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
2138 if (col > 1)
2139 curbuf->b_op_end.col = col - 1;
2140 else
2141 curbuf->b_op_end.col = 0;
2142
2143 if (flags & PUT_CURSLINE)
2144 {
2145 // ":put": put cursor on last inserted line
2146 curwin->w_cursor.lnum = lnum;
2147 beginline(BL_WHITE | BL_FIX);
2148 }
2149 else if (flags & PUT_CURSEND)
2150 {
2151 // put cursor after inserted text
2152 if (y_type == MLINE)
2153 {
2154 if (lnum >= curbuf->b_ml.ml_line_count)
2155 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2156 else
2157 curwin->w_cursor.lnum = lnum + 1;
2158 curwin->w_cursor.col = 0;
2159 }
2160 else
2161 {
2162 curwin->w_cursor.lnum = lnum;
2163 curwin->w_cursor.col = col;
2164 }
2165 }
2166 else if (y_type == MLINE)
2167 {
2168 // put cursor on first non-blank in first inserted line
2169 curwin->w_cursor.col = 0;
2170 if (dir == FORWARD)
2171 ++curwin->w_cursor.lnum;
2172 beginline(BL_WHITE | BL_FIX);
2173 }
2174 else // put cursor on first inserted character
2175 curwin->w_cursor = new_cursor;
2176 }
2177 }
2178
2179 msgmore(nr_lines);
2180 curwin->w_set_curswant = TRUE;
2181
2182end:
Bram Moolenaare1004402020-10-24 20:49:43 +02002183 if (cmdmod.cmod_flags & CMOD_LOCKMARKS)
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002184 {
2185 curbuf->b_op_start = orig_start;
2186 curbuf->b_op_end = orig_end;
2187 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002188 if (allocated)
2189 vim_free(insert_string);
2190 if (regname == '=')
2191 vim_free(y_array);
2192
2193 VIsual_active = FALSE;
2194
2195 // If the cursor is past the end of the line put it at the end.
2196 adjust_cursor_eol();
2197}
2198
2199/*
2200 * Return the character name of the register with the given number.
2201 */
2202 int
2203get_register_name(int num)
2204{
2205 if (num == -1)
2206 return '"';
2207 else if (num < 10)
2208 return num + '0';
2209 else if (num == DELETION_REGISTER)
2210 return '-';
2211#ifdef FEAT_CLIPBOARD
2212 else if (num == STAR_REGISTER)
2213 return '*';
2214 else if (num == PLUS_REGISTER)
2215 return '+';
2216#endif
2217 else
2218 {
2219#ifdef EBCDIC
2220 int i;
2221
2222 // EBCDIC is really braindead ...
2223 i = 'a' + (num - 10);
2224 if (i > 'i')
2225 i += 7;
2226 if (i > 'r')
2227 i += 8;
2228 return i;
2229#else
2230 return num + 'a' - 10;
2231#endif
2232 }
2233}
2234
2235/*
Bram Moolenaarbb861e22020-06-07 18:16:36 +02002236 * Return the index of the register "" points to.
2237 */
2238 int
2239get_unname_register()
2240{
2241 return y_previous == NULL ? -1 : y_previous - &y_regs[0];
2242}
2243
2244/*
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002245 * ":dis" and ":registers": Display the contents of the yank registers.
2246 */
2247 void
2248ex_display(exarg_T *eap)
2249{
2250 int i, n;
2251 long j;
2252 char_u *p;
2253 yankreg_T *yb;
2254 int name;
2255 int attr;
2256 char_u *arg = eap->arg;
2257 int clen;
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002258 int type;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002259
2260 if (arg != NULL && *arg == NUL)
2261 arg = NULL;
2262 attr = HL_ATTR(HLF_8);
2263
2264 // Highlight title
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002265 msg_puts_title(_("\nType Name Content"));
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002266 for (i = -1; i < NUM_REGISTERS && !got_int; ++i)
2267 {
2268 name = get_register_name(i);
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002269 switch (get_reg_type(name, NULL))
2270 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002271 case MLINE: type = 'l'; break;
2272 case MCHAR: type = 'c'; break;
2273 default: type = 'b'; break;
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002274 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002275 if (arg != NULL && vim_strchr(arg, name) == NULL
2276#ifdef ONE_CLIPBOARD
2277 // Star register and plus register contain the same thing.
2278 && (name != '*' || vim_strchr(arg, '+') == NULL)
2279#endif
2280 )
2281 continue; // did not ask for this register
2282
2283#ifdef FEAT_CLIPBOARD
2284 // Adjust register name for "unnamed" in 'clipboard'.
2285 // When it's a clipboard register, fill it with the current contents
2286 // of the clipboard.
2287 adjust_clip_reg(&name);
2288 (void)may_get_selection(name);
2289#endif
2290
2291 if (i == -1)
2292 {
2293 if (y_previous != NULL)
2294 yb = y_previous;
2295 else
2296 yb = &(y_regs[0]);
2297 }
2298 else
2299 yb = &(y_regs[i]);
2300
2301#ifdef FEAT_EVAL
2302 if (name == MB_TOLOWER(redir_reg)
2303 || (redir_reg == '"' && yb == y_previous))
2304 continue; // do not list register being written to, the
2305 // pointer can be freed
2306#endif
2307
2308 if (yb->y_array != NULL)
2309 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002310 int do_show = FALSE;
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002311
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002312 for (j = 0; !do_show && j < yb->y_size; ++j)
2313 do_show = !message_filtered(yb->y_array[j]);
2314
2315 if (do_show || yb->y_size == 0)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002316 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002317 msg_putchar('\n');
2318 msg_puts(" ");
2319 msg_putchar(type);
2320 msg_puts(" ");
2321 msg_putchar('"');
2322 msg_putchar(name);
2323 msg_puts(" ");
2324
2325 n = (int)Columns - 11;
2326 for (j = 0; j < yb->y_size && n > 1; ++j)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002327 {
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002328 if (j)
2329 {
2330 msg_puts_attr("^J", attr);
2331 n -= 2;
2332 }
2333 for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0;
2334 ++p)
2335 {
2336 clen = (*mb_ptr2len)(p);
2337 msg_outtrans_len(p, clen);
2338 p += clen - 1;
2339 }
2340 }
2341 if (n > 1 && yb->y_type == MLINE)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002342 msg_puts_attr("^J", attr);
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002343 out_flush(); // show one line at a time
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002344 }
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002345 ui_breakcheck();
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002346 }
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002347 }
2348
2349 // display last inserted text
2350 if ((p = get_last_insert()) != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002351 && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int
2352 && !message_filtered(p))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002353 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002354 msg_puts("\n c \". ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002355 dis_msg(p, TRUE);
2356 }
2357
2358 // display last command line
2359 if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002360 && !got_int && !message_filtered(last_cmdline))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002361 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002362 msg_puts("\n c \": ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002363 dis_msg(last_cmdline, FALSE);
2364 }
2365
2366 // display current file name
2367 if (curbuf->b_fname != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002368 && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int
2369 && !message_filtered(curbuf->b_fname))
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(curbuf->b_fname, FALSE);
2373 }
2374
2375 // display alternate file name
2376 if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int)
2377 {
2378 char_u *fname;
2379 linenr_T dummy;
2380
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002381 if (buflist_name_nr(0, &fname, &dummy) != FAIL
2382 && !message_filtered(fname))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002383 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002384 msg_puts("\n c \"# ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002385 dis_msg(fname, FALSE);
2386 }
2387 }
2388
2389 // display last search pattern
2390 if (last_search_pat() != NULL
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002391 && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int
2392 && !message_filtered(last_search_pat()))
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002393 {
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002394 msg_puts("\n c \"/ ");
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002395 dis_msg(last_search_pat(), FALSE);
2396 }
2397
2398#ifdef FEAT_EVAL
2399 // display last used expression
2400 if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL)
Bram Moolenaar8fc42962019-10-26 17:33:13 +02002401 && !got_int && !message_filtered(expr_line))
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(expr_line, FALSE);
2405 }
2406#endif
2407}
2408
2409/*
2410 * display a string for do_dis()
2411 * truncate at end of screen line
2412 */
2413 static void
2414dis_msg(
2415 char_u *p,
2416 int skip_esc) // if TRUE, ignore trailing ESC
2417{
2418 int n;
2419 int l;
2420
2421 n = (int)Columns - 6;
2422 while (*p != NUL
2423 && !(*p == ESC && skip_esc && *(p + 1) == NUL)
2424 && (n -= ptr2cells(p)) >= 0)
2425 {
2426 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
2427 {
2428 msg_outtrans_len(p, l);
2429 p += l;
2430 }
2431 else
2432 msg_outtrans_len(p++, 1);
2433 }
2434 ui_breakcheck();
2435}
2436
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002437#if defined(FEAT_DND) || defined(PROTO)
2438/*
2439 * Replace the contents of the '~' register with str.
2440 */
2441 void
2442dnd_yank_drag_data(char_u *str, long len)
2443{
2444 yankreg_T *curr;
2445
2446 curr = y_current;
2447 y_current = &y_regs[TILDE_REGISTER];
2448 free_yank_all();
2449 str_to_reg(y_current, MCHAR, str, len, 0L, FALSE);
2450 y_current = curr;
2451}
2452#endif
2453
2454
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002455/*
2456 * Return the type of a register.
2457 * Used for getregtype()
2458 * Returns MAUTO for error.
2459 */
2460 char_u
2461get_reg_type(int regname, long *reglen)
2462{
2463 switch (regname)
2464 {
2465 case '%': // file name
2466 case '#': // alternate file name
2467 case '=': // expression
2468 case ':': // last command line
2469 case '/': // last search-pattern
2470 case '.': // last inserted text
2471# ifdef FEAT_SEARCHPATH
2472 case Ctrl_F: // Filename under cursor
2473 case Ctrl_P: // Path under cursor, expand via "path"
2474# endif
2475 case Ctrl_W: // word under cursor
2476 case Ctrl_A: // WORD (mnemonic All) under cursor
2477 case '_': // black hole: always empty
2478 return MCHAR;
2479 }
2480
2481# ifdef FEAT_CLIPBOARD
2482 regname = may_get_selection(regname);
2483# endif
2484
2485 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2486 return MAUTO;
2487
2488 get_yank_register(regname, FALSE);
2489
2490 if (y_current->y_array != NULL)
2491 {
2492 if (reglen != NULL && y_current->y_type == MBLOCK)
2493 *reglen = y_current->y_width;
2494 return y_current->y_type;
2495 }
2496 return MAUTO;
2497}
2498
Bram Moolenaar3691f1e2019-10-24 20:17:00 +02002499#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002500/*
2501 * When "flags" has GREG_LIST return a list with text "s".
2502 * Otherwise just return "s".
2503 */
2504 static char_u *
2505getreg_wrap_one_line(char_u *s, int flags)
2506{
2507 if (flags & GREG_LIST)
2508 {
2509 list_T *list = list_alloc();
2510
2511 if (list != NULL)
2512 {
2513 if (list_append_string(list, NULL, -1) == FAIL)
2514 {
2515 list_free(list);
2516 return NULL;
2517 }
2518 list->lv_first->li_tv.vval.v_string = s;
2519 }
2520 return (char_u *)list;
2521 }
2522 return s;
2523}
2524
2525/*
Bram Moolenaard672dde2020-02-26 13:43:51 +01002526 * Return the contents of a register as a single allocated string or as a list.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002527 * Used for "@r" in expressions and for getreg().
2528 * Returns NULL for error.
2529 * Flags:
2530 * GREG_NO_EXPR Do not allow expression register
2531 * GREG_EXPR_SRC For the expression register: return expression itself,
2532 * not the result of its evaluation.
Bram Moolenaard672dde2020-02-26 13:43:51 +01002533 * GREG_LIST Return a list of lines instead of a single string.
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002534 */
2535 char_u *
2536get_reg_contents(int regname, int flags)
2537{
2538 long i;
2539 char_u *retval;
2540 int allocated;
2541 long len;
2542
2543 // Don't allow using an expression register inside an expression
2544 if (regname == '=')
2545 {
2546 if (flags & GREG_NO_EXPR)
2547 return NULL;
2548 if (flags & GREG_EXPR_SRC)
2549 return getreg_wrap_one_line(get_expr_line_src(), flags);
2550 return getreg_wrap_one_line(get_expr_line(), flags);
2551 }
2552
2553 if (regname == '@') // "@@" is used for unnamed register
2554 regname = '"';
2555
2556 // check for valid regname
2557 if (regname != NUL && !valid_yank_reg(regname, FALSE))
2558 return NULL;
2559
2560# ifdef FEAT_CLIPBOARD
2561 regname = may_get_selection(regname);
2562# endif
2563
2564 if (get_spec_reg(regname, &retval, &allocated, FALSE))
2565 {
2566 if (retval == NULL)
2567 return NULL;
2568 if (allocated)
2569 return getreg_wrap_one_line(retval, flags);
2570 return getreg_wrap_one_line(vim_strsave(retval), flags);
2571 }
2572
2573 get_yank_register(regname, FALSE);
2574 if (y_current->y_array == NULL)
2575 return NULL;
2576
2577 if (flags & GREG_LIST)
2578 {
2579 list_T *list = list_alloc();
2580 int error = FALSE;
2581
2582 if (list == NULL)
2583 return NULL;
2584 for (i = 0; i < y_current->y_size; ++i)
2585 if (list_append_string(list, y_current->y_array[i], -1) == FAIL)
2586 error = TRUE;
2587 if (error)
2588 {
2589 list_free(list);
2590 return NULL;
2591 }
2592 return (char_u *)list;
2593 }
2594
2595 // Compute length of resulting string.
2596 len = 0;
2597 for (i = 0; i < y_current->y_size; ++i)
2598 {
2599 len += (long)STRLEN(y_current->y_array[i]);
2600 // Insert a newline between lines and after last line if
2601 // y_type is MLINE.
2602 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2603 ++len;
2604 }
2605
2606 retval = alloc(len + 1);
2607
2608 // Copy the lines of the yank register into the string.
2609 if (retval != NULL)
2610 {
2611 len = 0;
2612 for (i = 0; i < y_current->y_size; ++i)
2613 {
2614 STRCPY(retval + len, y_current->y_array[i]);
2615 len += (long)STRLEN(retval + len);
2616
2617 // Insert a NL between lines and after the last line if y_type is
2618 // MLINE.
2619 if (y_current->y_type == MLINE || i < y_current->y_size - 1)
2620 retval[len++] = '\n';
2621 }
2622 retval[len] = NUL;
2623 }
2624
2625 return retval;
2626}
2627
2628 static int
2629init_write_reg(
2630 int name,
2631 yankreg_T **old_y_previous,
2632 yankreg_T **old_y_current,
2633 int must_append,
2634 int *yank_type UNUSED)
2635{
2636 if (!valid_yank_reg(name, TRUE)) // check for valid reg name
2637 {
2638 emsg_invreg(name);
2639 return FAIL;
2640 }
2641
2642 // Don't want to change the current (unnamed) register
2643 *old_y_previous = y_previous;
2644 *old_y_current = y_current;
2645
2646 get_yank_register(name, TRUE);
2647 if (!y_append && !must_append)
2648 free_yank_all();
2649 return OK;
2650}
2651
2652 static void
2653finish_write_reg(
2654 int name,
2655 yankreg_T *old_y_previous,
2656 yankreg_T *old_y_current)
2657{
2658# ifdef FEAT_CLIPBOARD
2659 // Send text of clipboard register to the clipboard.
2660 may_set_selection();
2661# endif
2662
2663 // ':let @" = "val"' should change the meaning of the "" register
2664 if (name != '"')
2665 y_previous = old_y_previous;
2666 y_current = old_y_current;
2667}
2668
2669/*
2670 * Store string "str" in register "name".
2671 * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
2672 * If "must_append" is TRUE, always append to the register. Otherwise append
2673 * if "name" is an uppercase letter.
2674 * Note: "maxlen" and "must_append" don't work for the "/" register.
2675 * Careful: 'str' is modified, you may have to use a copy!
2676 * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
2677 */
2678 void
2679write_reg_contents(
2680 int name,
2681 char_u *str,
2682 int maxlen,
2683 int must_append)
2684{
2685 write_reg_contents_ex(name, str, maxlen, must_append, MAUTO, 0L);
2686}
2687
2688 void
2689write_reg_contents_lst(
2690 int name,
2691 char_u **strings,
2692 int maxlen UNUSED,
2693 int must_append,
2694 int yank_type,
2695 long block_len)
2696{
2697 yankreg_T *old_y_previous, *old_y_current;
2698
2699 if (name == '/' || name == '=')
2700 {
2701 char_u *s;
2702
2703 if (strings[0] == NULL)
2704 s = (char_u *)"";
2705 else if (strings[1] != NULL)
2706 {
2707 emsg(_("E883: search pattern and expression register may not "
2708 "contain two or more lines"));
2709 return;
2710 }
2711 else
2712 s = strings[0];
2713 write_reg_contents_ex(name, s, -1, must_append, yank_type, block_len);
2714 return;
2715 }
2716
2717 if (name == '_') // black hole: nothing to do
2718 return;
2719
2720 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2721 &yank_type) == FAIL)
2722 return;
2723
2724 str_to_reg(y_current, yank_type, (char_u *) strings, -1, block_len, TRUE);
2725
2726 finish_write_reg(name, old_y_previous, old_y_current);
2727}
2728
2729 void
2730write_reg_contents_ex(
2731 int name,
2732 char_u *str,
2733 int maxlen,
2734 int must_append,
2735 int yank_type,
2736 long block_len)
2737{
2738 yankreg_T *old_y_previous, *old_y_current;
2739 long len;
2740
2741 if (maxlen >= 0)
2742 len = maxlen;
2743 else
2744 len = (long)STRLEN(str);
2745
2746 // Special case: '/' search pattern
2747 if (name == '/')
2748 {
2749 set_last_search_pat(str, RE_SEARCH, TRUE, TRUE);
2750 return;
2751 }
2752
2753 if (name == '#')
2754 {
2755 buf_T *buf;
2756
2757 if (VIM_ISDIGIT(*str))
2758 {
2759 int num = atoi((char *)str);
2760
2761 buf = buflist_findnr(num);
2762 if (buf == NULL)
2763 semsg(_(e_nobufnr), (long)num);
2764 }
2765 else
2766 buf = buflist_findnr(buflist_findpat(str, str + STRLEN(str),
2767 TRUE, FALSE, FALSE));
2768 if (buf == NULL)
2769 return;
2770 curwin->w_alt_fnum = buf->b_fnum;
2771 return;
2772 }
2773
2774 if (name == '=')
2775 {
2776 char_u *p, *s;
2777
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002778 p = vim_strnsave(str, len);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002779 if (p == NULL)
2780 return;
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002781 if (must_append && expr_line != NULL)
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002782 {
Bram Moolenaar6b649ac2019-12-07 17:47:22 +01002783 s = concat_str(expr_line, p);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002784 vim_free(p);
2785 p = s;
2786 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002787 set_expr_line(p, NULL);
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002788 return;
2789 }
2790
2791 if (name == '_') // black hole: nothing to do
2792 return;
2793
2794 if (init_write_reg(name, &old_y_previous, &old_y_current, must_append,
2795 &yank_type) == FAIL)
2796 return;
2797
2798 str_to_reg(y_current, yank_type, str, len, block_len, FALSE);
2799
2800 finish_write_reg(name, old_y_previous, old_y_current);
2801}
2802#endif // FEAT_EVAL
2803
2804#if defined(FEAT_CLIPBOARD) || defined(FEAT_EVAL)
2805/*
2806 * Put a string into a register. When the register is not empty, the string
2807 * is appended.
2808 */
Bram Moolenaar45fffdf2020-03-24 21:42:01 +01002809 void
Bram Moolenaar4aea03e2019-09-25 22:37:17 +02002810str_to_reg(
2811 yankreg_T *y_ptr, // pointer to yank register
2812 int yank_type, // MCHAR, MLINE, MBLOCK, MAUTO
2813 char_u *str, // string to put in register
2814 long len, // length of string
2815 long blocklen, // width of Visual block
2816 int str_list) // TRUE if str is char_u **
2817{
2818 int type; // MCHAR, MLINE or MBLOCK
2819 int lnum;
2820 long start;
2821 long i;
2822 int extra;
2823 int newlines; // number of lines added
2824 int extraline = 0; // extra line at the end
2825 int append = FALSE; // append to last line in register
2826 char_u *s;
2827 char_u **ss;
2828 char_u **pp;
2829 long maxlen;
2830
2831 if (y_ptr->y_array == NULL) // NULL means empty register
2832 y_ptr->y_size = 0;
2833
2834 if (yank_type == MAUTO)
2835 type = ((str_list || (len > 0 && (str[len - 1] == NL
2836 || str[len - 1] == CAR)))
2837 ? MLINE : MCHAR);
2838 else
2839 type = yank_type;
2840
2841 // Count the number of lines within the string
2842 newlines = 0;
2843 if (str_list)
2844 {
2845 for (ss = (char_u **) str; *ss != NULL; ++ss)
2846 ++newlines;
2847 }
2848 else
2849 {
2850 for (i = 0; i < len; i++)
2851 if (str[i] == '\n')
2852 ++newlines;
2853 if (type == MCHAR || len == 0 || str[len - 1] != '\n')
2854 {
2855 extraline = 1;
2856 ++newlines; // count extra newline at the end
2857 }
2858 if (y_ptr->y_size > 0 && y_ptr->y_type == MCHAR)
2859 {
2860 append = TRUE;
2861 --newlines; // uncount newline when appending first line
2862 }
2863 }
2864
2865 // Without any lines make the register empty.
2866 if (y_ptr->y_size + newlines == 0)
2867 {
2868 VIM_CLEAR(y_ptr->y_array);
2869 return;
2870 }
2871
2872 // Allocate an array to hold the pointers to the new register lines.
2873 // If the register was not empty, move the existing lines to the new array.
2874 pp = lalloc_clear((y_ptr->y_size + newlines) * sizeof(char_u *), TRUE);
2875 if (pp == NULL) // out of memory
2876 return;
2877 for (lnum = 0; lnum < y_ptr->y_size; ++lnum)
2878 pp[lnum] = y_ptr->y_array[lnum];
2879 vim_free(y_ptr->y_array);
2880 y_ptr->y_array = pp;
2881 maxlen = 0;
2882
2883 // Find the end of each line and save it into the array.
2884 if (str_list)
2885 {
2886 for (ss = (char_u **) str; *ss != NULL; ++ss, ++lnum)
2887 {
2888 i = (long)STRLEN(*ss);
2889 pp[lnum] = vim_strnsave(*ss, i);
2890 if (i > maxlen)
2891 maxlen = i;
2892 }
2893 }
2894 else
2895 {
2896 for (start = 0; start < len + extraline; start += i + 1)
2897 {
2898 for (i = start; i < len; ++i) // find the end of the line
2899 if (str[i] == '\n')
2900 break;
2901 i -= start; // i is now length of line
2902 if (i > maxlen)
2903 maxlen = i;
2904 if (append)
2905 {
2906 --lnum;
2907 extra = (int)STRLEN(y_ptr->y_array[lnum]);
2908 }
2909 else
2910 extra = 0;
2911 s = alloc(i + extra + 1);
2912 if (s == NULL)
2913 break;
2914 if (extra)
2915 mch_memmove(s, y_ptr->y_array[lnum], (size_t)extra);
2916 if (append)
2917 vim_free(y_ptr->y_array[lnum]);
2918 if (i)
2919 mch_memmove(s + extra, str + start, (size_t)i);
2920 extra += i;
2921 s[extra] = NUL;
2922 y_ptr->y_array[lnum++] = s;
2923 while (--extra >= 0)
2924 {
2925 if (*s == NUL)
2926 *s = '\n'; // replace NUL with newline
2927 ++s;
2928 }
2929 append = FALSE; // only first line is appended
2930 }
2931 }
2932 y_ptr->y_type = type;
2933 y_ptr->y_size = lnum;
2934 if (type == MBLOCK)
2935 y_ptr->y_width = (blocklen < 0 ? maxlen - 1 : blocklen);
2936 else
2937 y_ptr->y_width = 0;
2938# ifdef FEAT_VIMINFO
2939 y_ptr->y_time_set = vim_time();
2940# endif
2941}
2942#endif // FEAT_CLIPBOARD || FEAT_EVAL || PROTO