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