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