/
register.c
3052 lines (2756 loc) · 71.8 KB
/
register.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* register.c: functions for managing registers
*/
#include "vim.h"
/*
* Registers:
* 0 = unnamed register, for normal yanks and puts
* 1..9 = registers '1' to '9', for deletes
* 10..35 = registers 'a' to 'z' ('A' to 'Z' for appending)
* 36 = delete register '-'
* 37 = Selection register '*'. Only if FEAT_CLIPBOARD defined
* 38 = Clipboard register '+'. Only if FEAT_CLIPBOARD and FEAT_X11 defined
*/
static yankreg_T y_regs[NUM_REGISTERS];
static yankreg_T *y_current; // ptr to current yankreg
static int y_append; // TRUE when appending
static yankreg_T *y_previous = NULL; // ptr to last written yankreg
static int stuff_yank(int, char_u *);
static void put_reedit_in_typebuf(int silent);
static int put_in_typebuf(char_u *s, int esc, int colon,
int silent);
static int yank_copy_line(struct block_def *bd, long y_idx, int exclude_trailing_space);
#ifdef FEAT_CLIPBOARD
static void copy_yank_reg(yankreg_T *reg);
#endif
static void dis_msg(char_u *p, int skip_esc);
#if defined(FEAT_VIMINFO) || defined(PROTO)
yankreg_T *
get_y_regs(void)
{
return y_regs;
}
#endif
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
yankreg_T *
get_y_register(int reg)
{
return &y_regs[reg];
}
#endif
yankreg_T *
get_y_current(void)
{
return y_current;
}
yankreg_T *
get_y_previous(void)
{
return y_previous;
}
void
set_y_current(yankreg_T *yreg)
{
y_current = yreg;
}
void
set_y_previous(yankreg_T *yreg)
{
y_previous = yreg;
}
void
reset_y_append(void)
{
y_append = FALSE;
}
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Keep the last expression line here, for repeating.
*/
static char_u *expr_line = NULL;
static exarg_T *expr_eap = NULL;
/*
* Get an expression for the "\"=expr1" or "CTRL-R =expr1"
* Returns '=' when OK, NUL otherwise.
*/
int
get_expr_register(void)
{
char_u *new_line;
new_line = getcmdline('=', 0L, 0, 0);
if (new_line == NULL)
return NUL;
if (*new_line == NUL) // use previous line
vim_free(new_line);
else
set_expr_line(new_line, NULL);
return '=';
}
/*
* Set the expression for the '=' register.
* Argument must be an allocated string.
* "eap" may be used if the next line needs to be checked when evaluating the
* expression.
*/
void
set_expr_line(char_u *new_line, exarg_T *eap)
{
vim_free(expr_line);
expr_line = new_line;
expr_eap = eap;
}
/*
* Get the result of the '=' register expression.
* Returns a pointer to allocated memory, or NULL for failure.
*/
char_u *
get_expr_line(void)
{
char_u *expr_copy;
char_u *rv;
static int nested = 0;
if (expr_line == NULL)
return NULL;
// Make a copy of the expression, because evaluating it may cause it to be
// changed.
expr_copy = vim_strsave(expr_line);
if (expr_copy == NULL)
return NULL;
// When we are invoked recursively limit the evaluation to 10 levels.
// Then return the string as-is.
if (nested >= 10)
return expr_copy;
++nested;
rv = eval_to_string_eap(expr_copy, TRUE, expr_eap, FALSE);
--nested;
vim_free(expr_copy);
return rv;
}
/*
* Get the '=' register expression itself, without evaluating it.
*/
static char_u *
get_expr_line_src(void)
{
if (expr_line == NULL)
return NULL;
return vim_strsave(expr_line);
}
#endif // FEAT_EVAL
/*
* Check if 'regname' is a valid name of a yank register.
* Note: There is no check for 0 (default register), caller should do this
*/
int
valid_yank_reg(
int regname,
int writing) // if TRUE check for writable registers
{
if ( (regname > 0 && ASCII_ISALNUM(regname))
|| (!writing && vim_strchr((char_u *)
#ifdef FEAT_EVAL
"/.%:="
#else
"/.%:"
#endif
, regname) != NULL)
|| regname == '#'
|| regname == '"'
|| regname == '-'
|| regname == '_'
#ifdef FEAT_CLIPBOARD
|| regname == '*'
|| regname == '+'
#endif
#ifdef FEAT_DND
|| (!writing && regname == '~')
#endif
)
return TRUE;
return FALSE;
}
/*
* Set y_current and y_append, according to the value of "regname".
* Cannot handle the '_' register.
* Must only be called with a valid register name!
*
* If regname is 0 and writing, use register 0
* If regname is 0 and reading, use previous register
*
* Return TRUE when the register should be inserted literally (selection or
* clipboard).
*/
int
get_yank_register(int regname, int writing)
{
int i;
int ret = FALSE;
y_append = FALSE;
if ((regname == 0 || regname == '"') && !writing && y_previous != NULL)
{
y_current = y_previous;
return ret;
}
i = regname;
if (VIM_ISDIGIT(i))
i -= '0';
else if (ASCII_ISLOWER(i))
i = CharOrdLow(i) + 10;
else if (ASCII_ISUPPER(i))
{
i = CharOrdUp(i) + 10;
y_append = TRUE;
}
else if (regname == '-')
i = DELETION_REGISTER;
#ifdef FEAT_CLIPBOARD
// When selection is not available, use register 0 instead of '*'
else if (clip_star.available && regname == '*')
{
i = STAR_REGISTER;
ret = TRUE;
}
// When clipboard is not available, use register 0 instead of '+'
else if (clip_plus.available && regname == '+')
{
i = PLUS_REGISTER;
ret = TRUE;
}
#endif
#ifdef FEAT_DND
else if (!writing && regname == '~')
i = TILDE_REGISTER;
#endif
else // not 0-9, a-z, A-Z or '-': use register 0
i = 0;
y_current = &(y_regs[i]);
if (writing) // remember the register we write into for do_put()
y_previous = y_current;
return ret;
}
/*
* Obtain the contents of a "normal" register. The register is made empty.
* The returned pointer has allocated memory, use put_register() later.
*/
void *
get_register(
int name,
int copy) // make a copy, if FALSE make register empty.
{
yankreg_T *reg;
int i;
#ifdef FEAT_CLIPBOARD
// When Visual area changed, may have to update selection. Obtain the
// selection too.
if (name == '*' && clip_star.available)
{
if (clip_isautosel_star())
clip_update_selection(&clip_star);
may_get_selection(name);
}
if (name == '+' && clip_plus.available)
{
if (clip_isautosel_plus())
clip_update_selection(&clip_plus);
may_get_selection(name);
}
#endif
get_yank_register(name, 0);
reg = ALLOC_ONE(yankreg_T);
if (reg == NULL)
return (void *)NULL;
*reg = *y_current;
if (copy)
{
// If we run out of memory some or all of the lines are empty.
if (reg->y_size == 0 || y_current->y_array == NULL)
reg->y_array = NULL;
else
reg->y_array = ALLOC_MULT(char_u *, reg->y_size);
if (reg->y_array != NULL)
{
for (i = 0; i < reg->y_size; ++i)
reg->y_array[i] = vim_strsave(y_current->y_array[i]);
}
}
else
y_current->y_array = NULL;
return (void *)reg;
}
/*
* Put "reg" into register "name". Free any previous contents and "reg".
*/
void
put_register(int name, void *reg)
{
get_yank_register(name, 0);
free_yank_all();
*y_current = *(yankreg_T *)reg;
vim_free(reg);
#ifdef FEAT_CLIPBOARD
// Send text written to clipboard register to the clipboard.
may_set_selection();
#endif
}
#if defined(FEAT_CLIPBOARD) || defined(PROTO)
void
free_register(void *reg)
{
yankreg_T tmp;
tmp = *y_current;
*y_current = *(yankreg_T *)reg;
free_yank_all();
vim_free(reg);
*y_current = tmp;
}
#endif
/*
* return TRUE if the current yank register has type MLINE
*/
int
yank_register_mline(int regname)
{
if (regname != 0 && !valid_yank_reg(regname, FALSE))
return FALSE;
if (regname == '_') // black hole is always empty
return FALSE;
get_yank_register(regname, FALSE);
return (y_current->y_type == MLINE);
}
/*
* Start or stop recording into a yank register.
*
* Return FAIL for failure, OK otherwise.
*/
int
do_record(int c)
{
char_u *p;
static int regname;
yankreg_T *old_y_previous, *old_y_current;
int retval;
if (reg_recording == 0) // start recording
{
// registers 0-9, a-z and " are allowed
if (c < 0 || (!ASCII_ISALNUM(c) && c != '"'))
retval = FAIL;
else
{
reg_recording = c;
showmode();
regname = c;
retval = OK;
}
}
else // stop recording
{
// Get the recorded key hits. K_SPECIAL and CSI will be escaped, this
// needs to be removed again to put it in a register. exec_reg then
// adds the escaping back later.
reg_recording = 0;
msg("");
p = get_recorded();
if (p == NULL)
retval = FAIL;
else
{
// Remove escaping for CSI and K_SPECIAL in multi-byte chars.
vim_unescape_csi(p);
// We don't want to change the default register here, so save and
// restore the current register name.
old_y_previous = y_previous;
old_y_current = y_current;
retval = stuff_yank(regname, p);
y_previous = old_y_previous;
y_current = old_y_current;
}
}
return retval;
}
/*
* Stuff string "p" into yank register "regname" as a single line (append if
* uppercase). "p" must have been alloced.
*
* return FAIL for failure, OK otherwise
*/
static int
stuff_yank(int regname, char_u *p)
{
char_u *lp;
char_u **pp;
// check for read-only register
if (regname != 0 && !valid_yank_reg(regname, TRUE))
{
vim_free(p);
return FAIL;
}
if (regname == '_') // black hole: don't do anything
{
vim_free(p);
return OK;
}
get_yank_register(regname, TRUE);
if (y_append && y_current->y_array != NULL)
{
pp = &(y_current->y_array[y_current->y_size - 1]);
lp = alloc(STRLEN(*pp) + STRLEN(p) + 1);
if (lp == NULL)
{
vim_free(p);
return FAIL;
}
STRCPY(lp, *pp);
STRCAT(lp, p);
vim_free(p);
vim_free(*pp);
*pp = lp;
}
else
{
free_yank_all();
if ((y_current->y_array = ALLOC_ONE(char_u *)) == NULL)
{
vim_free(p);
return FAIL;
}
y_current->y_array[0] = p;
y_current->y_size = 1;
y_current->y_type = MCHAR; // used to be MLINE, why?
#ifdef FEAT_VIMINFO
y_current->y_time_set = vim_time();
#endif
}
return OK;
}
/*
* Last executed register (@ command)
*/
static int execreg_lastc = NUL;
#if defined(FEAT_VIMINFO) || defined(PROTO)
int
get_execreg_lastc(void)
{
return execreg_lastc;
}
void
set_execreg_lastc(int lastc)
{
execreg_lastc = lastc;
}
#endif
/*
* When executing a register as a series of ex-commands, if the
* line-continuation character is used for a line, then join it with one or
* more previous lines. Note that lines are processed backwards starting from
* the last line in the register.
*
* Arguments:
* lines - list of lines in the register
* idx - index of the line starting with \ or "\. Join this line with all the
* immediate predecessor lines that start with a \ and the first line
* that doesn't start with a \. Lines that start with a comment "\
* character are ignored.
*
* Returns the concatenated line. The index of the line that should be
* processed next is returned in idx.
*/
static char_u *
execreg_line_continuation(char_u **lines, long *idx)
{
garray_T ga;
long i = *idx;
char_u *p;
int cmd_start;
int cmd_end = i;
int j;
char_u *str;
ga_init2(&ga, sizeof(char_u), 400);
// search backwards to find the first line of this command.
// Any line not starting with \ or "\ is the start of the
// command.
while (--i > 0)
{
p = skipwhite(lines[i]);
if (*p != '\\' && (p[0] != '"' || p[1] != '\\' || p[2] != ' '))
break;
}
cmd_start = i;
// join all the lines
ga_concat(&ga, lines[cmd_start]);
for (j = cmd_start + 1; j <= cmd_end; j++)
{
p = skipwhite(lines[j]);
if (*p == '\\')
{
// Adjust the growsize to the current length to
// speed up concatenating many lines.
if (ga.ga_len > 400)
{
if (ga.ga_len > 8000)
ga.ga_growsize = 8000;
else
ga.ga_growsize = ga.ga_len;
}
ga_concat(&ga, p + 1);
}
}
ga_append(&ga, NUL);
str = vim_strsave(ga.ga_data);
ga_clear(&ga);
*idx = i;
return str;
}
/*
* Execute a yank register: copy it into the stuff buffer.
*
* Return FAIL for failure, OK otherwise.
*/
int
do_execreg(
int regname,
int colon, // insert ':' before each line
int addcr, // always add '\n' to end of line
int silent) // set "silent" flag in typeahead buffer
{
long i;
char_u *p;
int retval = OK;
int remap;
// repeat previous one
if (regname == '@')
{
if (execreg_lastc == NUL)
{
emsg(_(e_no_previously_used_register));
return FAIL;
}
regname = execreg_lastc;
}
// check for valid regname
if (regname == '%' || regname == '#' || !valid_yank_reg(regname, FALSE))
{
emsg_invreg(regname);
return FAIL;
}
execreg_lastc = regname;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
// black hole: don't stuff anything
if (regname == '_')
return OK;
// use last command line
if (regname == ':')
{
if (last_cmdline == NULL)
{
emsg(_(e_no_previous_command_line));
return FAIL;
}
// don't keep the cmdline containing @:
VIM_CLEAR(new_last_cmdline);
// Escape all control characters with a CTRL-V
p = vim_strsave_escaped_ext(last_cmdline,
(char_u *)"\001\002\003\004\005\006\007"
"\010\011\012\013\014\015\016\017"
"\020\021\022\023\024\025\026\027"
"\030\031\032\033\034\035\036\037",
Ctrl_V, FALSE);
if (p != NULL)
{
// When in Visual mode "'<,'>" will be prepended to the command.
// Remove it when it's already there.
if (VIsual_active && STRNCMP(p, "'<,'>", 5) == 0)
retval = put_in_typebuf(p + 5, TRUE, TRUE, silent);
else
retval = put_in_typebuf(p, TRUE, TRUE, silent);
}
vim_free(p);
}
#ifdef FEAT_EVAL
else if (regname == '=')
{
p = get_expr_line();
if (p == NULL)
return FAIL;
retval = put_in_typebuf(p, TRUE, colon, silent);
vim_free(p);
}
#endif
else if (regname == '.') // use last inserted text
{
p = get_last_insert_save();
if (p == NULL)
{
emsg(_(e_no_inserted_text_yet));
return FAIL;
}
retval = put_in_typebuf(p, FALSE, colon, silent);
vim_free(p);
}
else
{
get_yank_register(regname, FALSE);
if (y_current->y_array == NULL)
return FAIL;
// Disallow remapping for ":@r".
remap = colon ? REMAP_NONE : REMAP_YES;
// Insert lines into typeahead buffer, from last one to first one.
put_reedit_in_typebuf(silent);
for (i = y_current->y_size; --i >= 0; )
{
char_u *escaped;
char_u *str;
int free_str = FALSE;
// insert NL between lines and after last line if type is MLINE
if (y_current->y_type == MLINE || i < y_current->y_size - 1
|| addcr)
{
if (ins_typebuf((char_u *)"\n", remap, 0, TRUE, silent) == FAIL)
return FAIL;
}
// Handle line-continuation for :@<register>
str = y_current->y_array[i];
if (colon && i > 0)
{
p = skipwhite(str);
if (*p == '\\' || (p[0] == '"' && p[1] == '\\' && p[2] == ' '))
{
str = execreg_line_continuation(y_current->y_array, &i);
if (str == NULL)
return FAIL;
free_str = TRUE;
}
}
escaped = vim_strsave_escape_csi(str);
if (free_str)
vim_free(str);
if (escaped == NULL)
return FAIL;
retval = ins_typebuf(escaped, remap, 0, TRUE, silent);
vim_free(escaped);
if (retval == FAIL)
return FAIL;
if (colon && ins_typebuf((char_u *)":", remap, 0, TRUE, silent)
== FAIL)
return FAIL;
}
reg_executing = regname == 0 ? '"' : regname; // disable "q" command
}
return retval;
}
/*
* If "restart_edit" is not zero, put it in the typeahead buffer, so that it's
* used only after other typeahead has been processed.
*/
static void
put_reedit_in_typebuf(int silent)
{
char_u buf[3];
if (restart_edit == NUL)
return;
if (restart_edit == 'V')
{
buf[0] = 'g';
buf[1] = 'R';
buf[2] = NUL;
}
else
{
buf[0] = restart_edit == 'I' ? 'i' : restart_edit;
buf[1] = NUL;
}
if (ins_typebuf(buf, REMAP_NONE, 0, TRUE, silent) == OK)
restart_edit = NUL;
}
/*
* Insert register contents "s" into the typeahead buffer, so that it will be
* executed again.
* When "esc" is TRUE it is to be taken literally: Escape CSI characters and
* no remapping.
*/
static int
put_in_typebuf(
char_u *s,
int esc,
int colon, // add ':' before the line
int silent)
{
int retval = OK;
put_reedit_in_typebuf(silent);
if (colon)
retval = ins_typebuf((char_u *)"\n", REMAP_NONE, 0, TRUE, silent);
if (retval == OK)
{
char_u *p;
if (esc)
p = vim_strsave_escape_csi(s);
else
p = s;
if (p == NULL)
retval = FAIL;
else
retval = ins_typebuf(p, esc ? REMAP_NONE : REMAP_YES,
0, TRUE, silent);
if (esc)
vim_free(p);
}
if (colon && retval == OK)
retval = ins_typebuf((char_u *)":", REMAP_NONE, 0, TRUE, silent);
return retval;
}
/*
* Insert a yank register: copy it into the Read buffer.
* Used by CTRL-R command and middle mouse button in insert mode.
*
* return FAIL for failure, OK otherwise
*/
int
insert_reg(
int regname,
int literally_arg) // insert literally, not as if typed
{
long i;
int retval = OK;
char_u *arg;
int allocated;
int literally = literally_arg;
// It is possible to get into an endless loop by having CTRL-R a in
// register a and then, in insert mode, doing CTRL-R a.
// If you hit CTRL-C, the loop will be broken here.
ui_breakcheck();
if (got_int)
return FAIL;
// check for valid regname
if (regname != NUL && !valid_yank_reg(regname, FALSE))
return FAIL;
#ifdef FEAT_CLIPBOARD
regname = may_get_selection(regname);
#endif
if (regname == '.') // insert last inserted text
retval = stuff_inserted(NUL, 1L, TRUE);
else if (get_spec_reg(regname, &arg, &allocated, TRUE))
{
if (arg == NULL)
return FAIL;
stuffescaped(arg, literally);
if (allocated)
vim_free(arg);
}
else // name or number register
{
if (get_yank_register(regname, FALSE))
literally = TRUE;
if (y_current->y_array == NULL)
retval = FAIL;
else
{
for (i = 0; i < y_current->y_size; ++i)
{
if (regname == '-')
{
AppendCharToRedobuff(Ctrl_R);
AppendCharToRedobuff(regname);
do_put(regname, NULL, BACKWARD, 1L, PUT_CURSEND);
}
else
stuffescaped(y_current->y_array[i], literally);
// Insert a newline between lines and after last line if
// y_type is MLINE.
if (y_current->y_type == MLINE || i < y_current->y_size - 1)
stuffcharReadbuff('\n');
}
}
}
return retval;
}
/*
* If "regname" is a special register, return TRUE and store a pointer to its
* value in "argp".
*/
int
get_spec_reg(
int regname,
char_u **argp,
int *allocated, // return: TRUE when value was allocated
int errmsg) // give error message when failing
{
int cnt;
*argp = NULL;
*allocated = FALSE;
switch (regname)
{
case '%': // file name
if (errmsg)
check_fname(); // will give emsg if not set
*argp = curbuf->b_fname;
return TRUE;
case '#': // alternate file name
*argp = getaltfname(errmsg); // may give emsg if not set
return TRUE;
#ifdef FEAT_EVAL
case '=': // result of expression
*argp = get_expr_line();
*allocated = TRUE;
return TRUE;
#endif
case ':': // last command line
if (last_cmdline == NULL && errmsg)
emsg(_(e_no_previous_command_line));
*argp = last_cmdline;
return TRUE;
case '/': // last search-pattern
if (last_search_pat() == NULL && errmsg)
emsg(_(e_no_previous_regular_expression));
*argp = last_search_pat();
return TRUE;
case '.': // last inserted text
*argp = get_last_insert_save();
*allocated = TRUE;
if (*argp == NULL && errmsg)
emsg(_(e_no_inserted_text_yet));
return TRUE;
case Ctrl_F: // Filename under cursor
case Ctrl_P: // Path under cursor, expand via "path"
if (!errmsg)
return FALSE;
*argp = file_name_at_cursor(FNAME_MESS | FNAME_HYP
| (regname == Ctrl_P ? FNAME_EXP : 0), 1L, NULL);
*allocated = TRUE;
return TRUE;
case Ctrl_W: // word under cursor
case Ctrl_A: // WORD (mnemonic All) under cursor
if (!errmsg)
return FALSE;
cnt = find_ident_under_cursor(argp, regname == Ctrl_W
? (FIND_IDENT|FIND_STRING) : FIND_STRING);
*argp = cnt ? vim_strnsave(*argp, cnt) : NULL;
*allocated = TRUE;
return TRUE;
case Ctrl_L: // Line under cursor
if (!errmsg)
return FALSE;
*argp = ml_get_buf(curwin->w_buffer,
curwin->w_cursor.lnum, FALSE);
return TRUE;
case '_': // black hole: always empty
*argp = (char_u *)"";
return TRUE;
}
return FALSE;
}
/*
* Paste a yank register into the command line.
* Only for non-special registers.
* Used by CTRL-R command in command-line mode
* insert_reg() can't be used here, because special characters from the
* register contents will be interpreted as commands.
*
* return FAIL for failure, OK otherwise
*/
int
cmdline_paste_reg(
int regname,
int literally_arg, // Insert text literally instead of "as typed"
int remcr) // don't add CR characters
{
long i;
int literally = literally_arg;
if (get_yank_register(regname, FALSE))
literally = TRUE;
if (y_current->y_array == NULL)
return FAIL;
for (i = 0; i < y_current->y_size; ++i)
{
cmdline_paste_str(y_current->y_array[i], literally);
// Insert ^M between lines and after last line if type is MLINE.
// Don't do this when "remcr" is TRUE.
if ((y_current->y_type == MLINE || i < y_current->y_size - 1) && !remcr)
cmdline_paste_str((char_u *)"\r", literally);
// Check for CTRL-C, in case someone tries to paste a few thousand
// lines and gets bored.
ui_breakcheck();
if (got_int)
return FAIL;
}
return OK;
}
/*
* Shift the delete registers: "9 is cleared, "8 becomes "9, etc.
*/
void
shift_delete_registers(void)
{
int n;
y_current = &y_regs[9];
free_yank_all(); // free register nine
for (n = 9; n > 1; --n)
y_regs[n] = y_regs[n - 1];
y_current = &y_regs[1];
if (!y_append)
y_previous = y_current;
y_regs[1].y_array = NULL; // set register one to empty
}
#if defined(FEAT_EVAL)
void
yank_do_autocmd(oparg_T *oap, yankreg_T *reg)
{
static int recursive = FALSE;
dict_T *v_event;
list_T *list;