-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_cmds.c
5658 lines (5186 loc) · 141 KB
/
ex_cmds.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.
*/
/*
* ex_cmds.c: some functions for command line commands
*/
#include "vim.h"
#include "version.h"
#include <float.h>
static int linelen(int *has_tab);
static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, int do_in, int do_out);
static int not_writing(void);
static int check_readonly(int *forceit, buf_T *buf);
static void delbuf_msg(char_u *name);
/*
* ":ascii" and "ga".
*/
void
do_ascii(exarg_T *eap UNUSED)
{
int c;
int cval;
char buf1[20];
char buf2[20];
char_u buf3[7];
#ifdef FEAT_DIGRAPHS
char_u *dig;
#endif
int cc[MAX_MCO];
int ci = 0;
int len;
if (enc_utf8)
c = utfc_ptr2char(ml_get_cursor(), cc);
else
c = gchar_cursor();
if (c == NUL)
{
msg("NUL");
return;
}
IObuff[0] = NUL;
if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80)
{
if (c == NL) // NUL is stored as NL
c = NUL;
if (c == CAR && get_fileformat(curbuf) == EOL_MAC)
cval = NL; // NL is stored as CR
else
cval = c;
if (vim_isprintc_strict(c) && (c < ' ' || c > '~'))
{
transchar_nonprint(curbuf, buf3, c);
vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3);
}
else
buf1[0] = NUL;
if (c >= 0x80)
vim_snprintf(buf2, sizeof(buf2), " <M-%s>",
(char *)transchar(c & 0x7f));
else
buf2[0] = NUL;
#ifdef FEAT_DIGRAPHS
dig = get_digraph_for_char(cval);
if (dig != NULL)
vim_snprintf((char *)IObuff, IOSIZE,
_("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
transchar(c), buf1, buf2, cval, cval, cval, dig);
else
#endif
vim_snprintf((char *)IObuff, IOSIZE,
_("<%s>%s%s %d, Hex %02x, Octal %03o"),
transchar(c), buf1, buf2, cval, cval, cval);
if (enc_utf8)
c = cc[ci++];
else
c = 0;
}
// Repeat for combining characters.
while (has_mbyte && (c >= 0x100 || (enc_utf8 && c >= 0x80)))
{
len = (int)STRLEN(IObuff);
// This assumes every multi-byte char is printable...
if (len > 0)
IObuff[len++] = ' ';
IObuff[len++] = '<';
if (enc_utf8 && utf_iscomposing(c)
#ifdef USE_GUI
&& !gui.in_use
#endif
)
IObuff[len++] = ' '; // draw composing char on top of a space
len += (*mb_char2bytes)(c, IObuff + len);
#ifdef FEAT_DIGRAPHS
dig = get_digraph_for_char(c);
if (dig != NULL)
vim_snprintf((char *)IObuff + len, IOSIZE - len,
c < 0x10000 ? _("> %d, Hex %04x, Oct %o, Digr %s")
: _("> %d, Hex %08x, Oct %o, Digr %s"),
c, c, c, dig);
else
#endif
vim_snprintf((char *)IObuff + len, IOSIZE - len,
c < 0x10000 ? _("> %d, Hex %04x, Octal %o")
: _("> %d, Hex %08x, Octal %o"),
c, c, c);
if (ci == MAX_MCO)
break;
if (enc_utf8)
c = cc[ci++];
else
c = 0;
}
msg((char *)IObuff);
}
/*
* ":left", ":center" and ":right": align text.
*/
void
ex_align(exarg_T *eap)
{
pos_T save_curpos;
int len;
int indent = 0;
int new_indent;
int has_tab;
int width;
#ifdef FEAT_RIGHTLEFT
if (curwin->w_p_rl)
{
// switch left and right aligning
if (eap->cmdidx == CMD_right)
eap->cmdidx = CMD_left;
else if (eap->cmdidx == CMD_left)
eap->cmdidx = CMD_right;
}
#endif
width = atoi((char *)eap->arg);
save_curpos = curwin->w_cursor;
if (eap->cmdidx == CMD_left) // width is used for new indent
{
if (width >= 0)
indent = width;
}
else
{
/*
* if 'textwidth' set, use it
* else if 'wrapmargin' set, use it
* if invalid value, use 80
*/
if (width <= 0)
width = curbuf->b_p_tw;
if (width == 0 && curbuf->b_p_wm > 0)
width = curwin->w_width - curbuf->b_p_wm;
if (width <= 0)
width = 80;
}
if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
return;
for (curwin->w_cursor.lnum = eap->line1;
curwin->w_cursor.lnum <= eap->line2; ++curwin->w_cursor.lnum)
{
if (eap->cmdidx == CMD_left) // left align
new_indent = indent;
else
{
has_tab = FALSE; // avoid uninit warnings
len = linelen(eap->cmdidx == CMD_right ? &has_tab
: NULL) - get_indent();
if (len <= 0) // skip blank lines
continue;
if (eap->cmdidx == CMD_center)
new_indent = (width - len) / 2;
else
{
new_indent = width - len; // right align
/*
* Make sure that embedded TABs don't make the text go too far
* to the right.
*/
if (has_tab)
while (new_indent > 0)
{
(void)set_indent(new_indent, 0);
if (linelen(NULL) <= width)
{
/*
* Now try to move the line as much as possible to
* the right. Stop when it moves too far.
*/
do
(void)set_indent(++new_indent, 0);
while (linelen(NULL) <= width);
--new_indent;
break;
}
--new_indent;
}
}
}
if (new_indent < 0)
new_indent = 0;
(void)set_indent(new_indent, 0); // set indent
}
changed_lines(eap->line1, 0, eap->line2 + 1, 0L);
curwin->w_cursor = save_curpos;
beginline(BL_WHITE | BL_FIX);
}
/*
* Get the length of the current line, excluding trailing white space.
*/
static int
linelen(int *has_tab)
{
char_u *line;
char_u *first;
char_u *last;
int save;
int len;
// Get the line. If it's empty bail out early (could be the empty string
// for an unloaded buffer).
line = ml_get_curline();
if (*line == NUL)
return 0;
// find the first non-blank character
first = skipwhite(line);
// find the character after the last non-blank character
for (last = first + STRLEN(first);
last > first && VIM_ISWHITE(last[-1]); --last)
;
save = *last;
*last = NUL;
len = linetabsize_str(line); // get line length on screen
if (has_tab != NULL) // check for embedded TAB
*has_tab = (vim_strchr(first, TAB) != NULL);
*last = save;
return len;
}
// Buffer for two lines used during sorting. They are allocated to
// contain the longest line being sorted.
static char_u *sortbuf1;
static char_u *sortbuf2;
static int sort_lc; // sort using locale
static int sort_ic; // ignore case
static int sort_nr; // sort on number
static int sort_rx; // sort on regex instead of skipping it
static int sort_flt; // sort on floating number
static int sort_abort; // flag to indicate if sorting has been interrupted
// Struct to store info to be sorted.
typedef struct
{
linenr_T lnum; // line number
union {
struct
{
varnumber_T start_col_nr; // starting column number
varnumber_T end_col_nr; // ending column number
} line;
struct
{
varnumber_T value; // value if sorting by integer
int is_number; // TRUE when line contains a number
} num;
float_T value_flt; // value if sorting by float
} st_u;
} sorti_T;
static int
string_compare(const void *s1, const void *s2)
{
if (sort_lc)
return strcoll((char *)s1, (char *)s2);
return sort_ic ? STRICMP(s1, s2) : STRCMP(s1, s2);
}
static int
sort_compare(const void *s1, const void *s2)
{
sorti_T l1 = *(sorti_T *)s1;
sorti_T l2 = *(sorti_T *)s2;
int result = 0;
// If the user interrupts, there's no way to stop qsort() immediately, but
// if we return 0 every time, qsort will assume it's done sorting and
// exit.
if (sort_abort)
return 0;
fast_breakcheck();
if (got_int)
sort_abort = TRUE;
if (sort_nr)
{
if (l1.st_u.num.is_number != l2.st_u.num.is_number)
result = l1.st_u.num.is_number - l2.st_u.num.is_number;
else
result = l1.st_u.num.value == l2.st_u.num.value ? 0
: l1.st_u.num.value > l2.st_u.num.value ? 1 : -1;
}
else if (sort_flt)
result = l1.st_u.value_flt == l2.st_u.value_flt ? 0
: l1.st_u.value_flt > l2.st_u.value_flt ? 1 : -1;
else
{
// We need to copy one line into "sortbuf1", because there is no
// guarantee that the first pointer becomes invalid when obtaining the
// second one.
STRNCPY(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr + 1);
sortbuf1[l1.st_u.line.end_col_nr - l1.st_u.line.start_col_nr] = 0;
STRNCPY(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr + 1);
sortbuf2[l2.st_u.line.end_col_nr - l2.st_u.line.start_col_nr] = 0;
result = string_compare(sortbuf1, sortbuf2);
}
// If two lines have the same value, preserve the original line order.
if (result == 0)
return (int)(l1.lnum - l2.lnum);
return result;
}
/*
* ":sort".
*/
void
ex_sort(exarg_T *eap)
{
regmatch_T regmatch;
int len;
linenr_T lnum;
long maxlen = 0;
sorti_T *nrs;
size_t count = (size_t)(eap->line2 - eap->line1 + 1);
size_t i;
char_u *p;
char_u *s;
char_u *s2;
char_u c; // temporary character storage
int unique = FALSE;
long deleted;
colnr_T start_col;
colnr_T end_col;
int sort_what = 0;
int format_found = 0;
int change_occurred = FALSE; // Buffer contents changed.
// Sorting one line is really quick!
if (count <= 1)
return;
if (u_save((linenr_T)(eap->line1 - 1), (linenr_T)(eap->line2 + 1)) == FAIL)
return;
sortbuf1 = NULL;
sortbuf2 = NULL;
regmatch.regprog = NULL;
nrs = ALLOC_MULT(sorti_T, count);
if (nrs == NULL)
goto sortend;
sort_abort = sort_ic = sort_lc = sort_rx = sort_nr = 0;
sort_flt = 0;
for (p = eap->arg; *p != NUL; ++p)
{
if (VIM_ISWHITE(*p))
;
else if (*p == 'i')
sort_ic = TRUE;
else if (*p == 'l')
sort_lc = TRUE;
else if (*p == 'r')
sort_rx = TRUE;
else if (*p == 'n')
{
sort_nr = 1;
++format_found;
}
else if (*p == 'f')
{
sort_flt = 1;
++format_found;
}
else if (*p == 'b')
{
sort_what = STR2NR_BIN + STR2NR_FORCE;
++format_found;
}
else if (*p == 'o')
{
sort_what = STR2NR_OCT + STR2NR_FORCE;
++format_found;
}
else if (*p == 'x')
{
sort_what = STR2NR_HEX + STR2NR_FORCE;
++format_found;
}
else if (*p == 'u')
unique = TRUE;
else if (*p == '"') // comment start
break;
else if (eap->nextcmd == NULL && check_nextcmd(p) != NULL)
{
eap->nextcmd = check_nextcmd(p);
break;
}
else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL)
{
s = skip_regexp_err(p + 1, *p, TRUE);
if (s == NULL)
goto sortend;
*s = NUL;
// Use last search pattern if sort pattern is empty.
if (s == p + 1)
{
if (last_search_pat() == NULL)
{
emsg(_(e_no_previous_regular_expression));
goto sortend;
}
regmatch.regprog = vim_regcomp(last_search_pat(), RE_MAGIC);
}
else
regmatch.regprog = vim_regcomp(p + 1, RE_MAGIC);
if (regmatch.regprog == NULL)
goto sortend;
p = s; // continue after the regexp
regmatch.rm_ic = p_ic;
}
else
{
semsg(_(e_invalid_argument_str), p);
goto sortend;
}
}
// Can only have one of 'n', 'b', 'o' and 'x'.
if (format_found > 1)
{
emsg(_(e_invalid_argument));
goto sortend;
}
// From here on "sort_nr" is used as a flag for any integer number
// sorting.
sort_nr += sort_what;
/*
* Make an array with all line numbers. This avoids having to copy all
* the lines into allocated memory.
* When sorting on strings "start_col_nr" is the offset in the line, for
* numbers sorting it's the number to sort on. This means the pattern
* matching and number conversion only has to be done once per line.
* Also get the longest line length for allocating "sortbuf".
*/
for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
{
s = ml_get(lnum);
len = (int)STRLEN(s);
if (maxlen < len)
maxlen = len;
start_col = 0;
end_col = len;
if (regmatch.regprog != NULL && vim_regexec(®match, s, 0))
{
if (sort_rx)
{
start_col = (colnr_T)(regmatch.startp[0] - s);
end_col = (colnr_T)(regmatch.endp[0] - s);
}
else
start_col = (colnr_T)(regmatch.endp[0] - s);
}
else
if (regmatch.regprog != NULL)
end_col = 0;
if (sort_nr || sort_flt)
{
// Make sure vim_str2nr() doesn't read any digits past the end
// of the match, by temporarily terminating the string there
s2 = s + end_col;
c = *s2;
*s2 = NUL;
// Sorting on number: Store the number itself.
p = s + start_col;
if (sort_nr)
{
if (sort_what & STR2NR_HEX)
s = skiptohex(p);
else if (sort_what & STR2NR_BIN)
s = skiptobin(p);
else
s = skiptodigit(p);
if (s > p && s[-1] == '-')
--s; // include preceding negative sign
if (*s == NUL)
{
// line without number should sort before any number
nrs[lnum - eap->line1].st_u.num.is_number = FALSE;
nrs[lnum - eap->line1].st_u.num.value = 0;
}
else
{
nrs[lnum - eap->line1].st_u.num.is_number = TRUE;
vim_str2nr(s, NULL, NULL, sort_what,
&nrs[lnum - eap->line1].st_u.num.value,
NULL, 0, FALSE, NULL);
}
}
else
{
s = skipwhite(p);
if (*s == '+')
s = skipwhite(s + 1);
if (*s == NUL)
// empty line should sort before any number
nrs[lnum - eap->line1].st_u.value_flt = -DBL_MAX;
else
nrs[lnum - eap->line1].st_u.value_flt =
strtod((char *)s, NULL);
}
*s2 = c;
}
else
{
// Store the column to sort at.
nrs[lnum - eap->line1].st_u.line.start_col_nr = start_col;
nrs[lnum - eap->line1].st_u.line.end_col_nr = end_col;
}
nrs[lnum - eap->line1].lnum = lnum;
if (regmatch.regprog != NULL)
fast_breakcheck();
if (got_int)
goto sortend;
}
// Allocate a buffer that can hold the longest line.
sortbuf1 = alloc(maxlen + 1);
if (sortbuf1 == NULL)
goto sortend;
sortbuf2 = alloc(maxlen + 1);
if (sortbuf2 == NULL)
goto sortend;
// Sort the array of line numbers. Note: can't be interrupted!
qsort((void *)nrs, count, sizeof(sorti_T), sort_compare);
if (sort_abort)
goto sortend;
// Insert the lines in the sorted order below the last one.
lnum = eap->line2;
for (i = 0; i < count; ++i)
{
linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum;
// If the original line number of the line being placed is not the same
// as "lnum" (accounting for offset), we know that the buffer changed.
if (get_lnum + ((linenr_T)count - 1) != lnum)
change_occurred = TRUE;
s = ml_get(get_lnum);
if (!unique || i == 0 || string_compare(s, sortbuf1) != 0)
{
// Copy the line into a buffer, it may become invalid in
// ml_append(). And it's needed for "unique".
STRCPY(sortbuf1, s);
if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
break;
}
fast_breakcheck();
if (got_int)
goto sortend;
}
// delete the original lines if appending worked
if (i == count)
for (i = 0; i < count; ++i)
ml_delete(eap->line1);
else
count = 0;
// Adjust marks for deleted (or added) lines and prepare for displaying.
deleted = (long)(count - (lnum - eap->line2));
if (deleted > 0)
{
mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
msgmore(-deleted);
}
else if (deleted < 0)
mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
if (change_occurred || deleted != 0)
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
curwin->w_cursor.lnum = eap->line1;
beginline(BL_WHITE | BL_FIX);
sortend:
vim_free(nrs);
vim_free(sortbuf1);
vim_free(sortbuf2);
vim_regfree(regmatch.regprog);
if (got_int)
emsg(_(e_interrupted));
}
/*
* :move command - move lines line1-line2 to line dest
*
* return FAIL for failure, OK otherwise
*/
int
do_move(linenr_T line1, linenr_T line2, linenr_T dest)
{
char_u *str;
linenr_T l;
linenr_T extra; // Num lines added before line1
linenr_T num_lines; // Num lines moved
linenr_T last_line; // Last line in file after adding new text
#ifdef FEAT_FOLDING
win_T *win;
tabpage_T *tp;
#endif
if (dest >= line1 && dest < line2)
{
emsg(_(e_cannot_move_range_of_lines_into_itself));
return FAIL;
}
// Do nothing if we are not actually moving any lines. This will prevent
// the 'modified' flag from being set without cause.
if (dest == line1 - 1 || dest == line2)
{
// Move the cursor as if lines were moved (see below) to be backwards
// compatible.
if (dest >= line1)
curwin->w_cursor.lnum = dest;
else
curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
return OK;
}
num_lines = line2 - line1 + 1;
/*
* First we copy the old text to its new location -- webb
* Also copy the flag that ":global" command uses.
*/
if (u_save(dest, dest + 1) == FAIL)
return FAIL;
for (extra = 0, l = line1; l <= line2; l++)
{
str = vim_strsave(ml_get(l + extra));
if (str != NULL)
{
ml_append(dest + l - line1, str, (colnr_T)0, FALSE);
vim_free(str);
if (dest < line1)
extra++;
}
}
/*
* Now we must be careful adjusting our marks so that we don't overlap our
* mark_adjust() calls.
*
* We adjust the marks within the old text so that they refer to the
* last lines of the file (temporarily), because we know no other marks
* will be set there since these line numbers did not exist until we added
* our new lines.
*
* Then we adjust the marks on lines between the old and new text positions
* (either forwards or backwards).
*
* And Finally we adjust the marks we put at the end of the file back to
* their final destination at the new text position -- webb
*/
last_line = curbuf->b_ml.ml_line_count;
mark_adjust_nofold(line1, line2, last_line - line2, 0L);
if (dest >= line2)
{
mark_adjust_nofold(line2 + 1, dest, -num_lines, 0L);
#ifdef FEAT_FOLDING
FOR_ALL_TAB_WINDOWS(tp, win)
{
if (win->w_buffer == curbuf)
foldMoveRange(&win->w_folds, line1, line2, dest);
}
#endif
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
{
curbuf->b_op_start.lnum = dest - num_lines + 1;
curbuf->b_op_end.lnum = dest;
}
}
else
{
mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0L);
#ifdef FEAT_FOLDING
FOR_ALL_TAB_WINDOWS(tp, win)
{
if (win->w_buffer == curbuf)
foldMoveRange(&win->w_folds, dest + 1, line1 - 1, line2);
}
#endif
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
{
curbuf->b_op_start.lnum = dest + 1;
curbuf->b_op_end.lnum = dest + num_lines;
}
}
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
mark_adjust_nofold(last_line - num_lines + 1, last_line,
-(last_line - dest - extra), 0L);
/*
* Now we delete the original text -- webb
*/
if (u_save(line1 + extra - 1, line2 + extra + 1) == FAIL)
return FAIL;
for (l = line1; l <= line2; l++)
ml_delete_flags(line1 + extra, ML_DEL_MESSAGE);
if (!global_busy && num_lines > p_report)
smsg(NGETTEXT("%ld line moved", "%ld lines moved", num_lines),
(long)num_lines);
/*
* Leave the cursor on the last of the moved lines.
*/
if (dest >= line1)
curwin->w_cursor.lnum = dest;
else
curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
if (line1 < dest)
{
dest += num_lines + 1;
last_line = curbuf->b_ml.ml_line_count;
if (dest > last_line + 1)
dest = last_line + 1;
changed_lines(line1, 0, dest, 0L);
}
else
changed_lines(dest + 1, 0, line1 + num_lines, 0L);
return OK;
}
/*
* ":copy"
*/
void
ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
{
linenr_T count;
char_u *p;
count = line2 - line1 + 1;
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0)
{
curbuf->b_op_start.lnum = n + 1;
curbuf->b_op_end.lnum = n + count;
curbuf->b_op_start.col = curbuf->b_op_end.col = 0;
}
/*
* there are three situations:
* 1. destination is above line1
* 2. destination is between line1 and line2
* 3. destination is below line2
*
* n = destination (when starting)
* curwin->w_cursor.lnum = destination (while copying)
* line1 = start of source (while copying)
* line2 = end of source (while copying)
*/
if (u_save(n, n + 1) == FAIL)
return;
curwin->w_cursor.lnum = n;
while (line1 <= line2)
{
// need to use vim_strsave() because the line will be unlocked within
// ml_append()
p = vim_strsave(ml_get(line1));
if (p != NULL)
{
ml_append(curwin->w_cursor.lnum, p, (colnr_T)0, FALSE);
vim_free(p);
}
// situation 2: skip already copied lines
if (line1 == n)
line1 = curwin->w_cursor.lnum;
++line1;
if (curwin->w_cursor.lnum < line1)
++line1;
if (curwin->w_cursor.lnum < line2)
++line2;
++curwin->w_cursor.lnum;
}
appended_lines_mark(n, count);
if (VIsual_active)
check_pos(curbuf, &VIsual);
msgmore((long)count);
}
static char_u *prevcmd = NULL; // the previous command
#if defined(EXITFREE) || defined(PROTO)
void
free_prev_shellcmd(void)
{
vim_free(prevcmd);
}
#endif
/*
* Check that "prevcmd" is not NULL. If it is NULL then give an error message
* and return FALSE.
*/
static int
prevcmd_is_set(void)
{
if (prevcmd == NULL)
{
emsg(_(e_no_previous_command));
return FALSE;
}
return TRUE;
}
/*
* Handle the ":!cmd" command. Also for ":r !cmd" and ":w !cmd"
* Bangs in the argument are replaced with the previously entered command.
* Remember the argument.
*/
void
do_bang(
int addr_count,
exarg_T *eap,
int forceit,
int do_in,
int do_out)
{
char_u *arg = eap->arg; // command
linenr_T line1 = eap->line1; // start of range
linenr_T line2 = eap->line2; // end of range
char_u *newcmd = NULL; // the new command
int free_newcmd = FALSE; // need to free() newcmd
int ins_prevcmd;
char_u *t;
char_u *p;
char_u *trailarg;
int len;
int scroll_save = msg_scroll;
/*
* Disallow shell commands for "rvim".
* Disallow shell commands from .exrc and .vimrc in current directory for
* security reasons.
*/
if (check_restricted() || check_secure())
return;
if (addr_count == 0) // :!
{
msg_scroll = FALSE; // don't scroll here
autowrite_all();
msg_scroll = scroll_save;
}
/*
* Try to find an embedded bang, like in ":!<cmd> ! [args]"
* ":!!" is indicated by the 'forceit' variable.
*/
ins_prevcmd = forceit;
// Skip leading white space to avoid a strange error with some shells.
trailarg = skipwhite(arg);
do
{
len = (int)STRLEN(trailarg) + 1;
if (newcmd != NULL)
len += (int)STRLEN(newcmd);
if (ins_prevcmd)
{
if (!prevcmd_is_set())
{
vim_free(newcmd);
return;
}
len += (int)STRLEN(prevcmd);
}
if ((t = alloc(len)) == NULL)
{
vim_free(newcmd);
return;
}
*t = NUL;
if (newcmd != NULL)
STRCAT(t, newcmd);
if (ins_prevcmd)
STRCAT(t, prevcmd);
p = t + STRLEN(t);
STRCAT(t, trailarg);
vim_free(newcmd);
newcmd = t;
/*
* Scan the rest of the argument for '!', which is replaced by the
* previous command. "\!" is replaced by "!" (this is vi compatible).
*/
trailarg = NULL;
while (*p)
{
if (*p == '!')
{
if (p > newcmd && p[-1] == '\\')
STRMOVE(p - 1, p);
else
{
trailarg = p;
*trailarg++ = NUL;
ins_prevcmd = TRUE;
break;
}
}
++p;
}
} while (trailarg != NULL);
// Only set "prevcmd" if there is a command to run, otherwise keep te one
// we have.
if (STRLEN(newcmd) > 0)
{
vim_free(prevcmd);
prevcmd = newcmd;
}
else
free_newcmd = TRUE;
if (bangredo) // put cmd in redo buffer for ! command
{
if (!prevcmd_is_set())
goto theend;
// If % or # appears in the command, it must have been escaped.
// Reescape them, so that redoing them does not substitute them by the
// buffername.
char_u *cmd = vim_strsave_escaped(prevcmd, (char_u *)"%#");
if (cmd != NULL)
{
AppendToRedobuffLit(cmd, -1);