forked from neovim/neovim
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex_cmds.c
4826 lines (4338 loc) · 149 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
// ex_cmds.c: some functions for command line commands
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "auto/config.h"
#include "klib/kvec.h"
#include "nvim/arglist.h"
#include "nvim/ascii_defs.h"
#include "nvim/autocmd.h"
#include "nvim/autocmd_defs.h"
#include "nvim/buffer.h"
#include "nvim/buffer_defs.h"
#include "nvim/buffer_updates.h"
#include "nvim/bufwrite.h"
#include "nvim/change.h"
#include "nvim/channel.h"
#include "nvim/charset.h"
#include "nvim/cmdexpand_defs.h"
#include "nvim/cmdhist.h"
#include "nvim/cursor.h"
#include "nvim/decoration.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/drawscreen.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/typval_defs.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_cmds_defs.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/extmark.h"
#include "nvim/extmark_defs.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/getchar.h"
#include "nvim/gettext_defs.h"
#include "nvim/globals.h"
#include "nvim/help.h"
#include "nvim/highlight.h"
#include "nvim/highlight_defs.h"
#include "nvim/highlight_group.h"
#include "nvim/indent.h"
#include "nvim/input.h"
#include "nvim/macros_defs.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/mark_defs.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memline_defs.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/mouse.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/ops.h"
#include "nvim/option.h"
#include "nvim/option_defs.h"
#include "nvim/option_vars.h"
#include "nvim/optionstr.h"
#include "nvim/os/fs.h"
#include "nvim/os/fs_defs.h"
#include "nvim/os/input.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/os/shell.h"
#include "nvim/os/time.h"
#include "nvim/path.h"
#include "nvim/plines.h"
#include "nvim/pos_defs.h"
#include "nvim/profile.h"
#include "nvim/quickfix.h"
#include "nvim/regexp.h"
#include "nvim/regexp_defs.h"
#include "nvim/search.h"
#include "nvim/spell.h"
#include "nvim/state_defs.h"
#include "nvim/strings.h"
#include "nvim/terminal.h"
#include "nvim/types_defs.h"
#include "nvim/ui.h"
#include "nvim/ui_defs.h"
#include "nvim/undo.h"
#include "nvim/vim_defs.h"
#include "nvim/window.h"
/// Case matching style to use for :substitute
typedef enum {
kSubHonorOptions = 0, ///< Honor the user's 'ignorecase'/'smartcase' options
kSubIgnoreCase, ///< Ignore case of the search
kSubMatchCase, ///< Match case of the search
} SubIgnoreType;
/// Flags kept between calls to :substitute.
typedef struct {
bool do_all; ///< do multiple substitutions per line
bool do_ask; ///< ask for confirmation
bool do_count; ///< count only
bool do_error; ///< if false, ignore errors
bool do_print; ///< print last line with subs
bool do_list; ///< list last line with subs
bool do_number; ///< list last line with line nr
SubIgnoreType do_ic; ///< ignore case flag
} subflags_T;
/// Partial result of a substitution during :substitute.
/// Numbers refer to the buffer _after_ substitution
typedef struct {
lpos_T start; // start of the match
lpos_T end; // end of the match
linenr_T pre_match; // where to begin showing lines before the match
} SubResult;
// Collected results of a substitution for showing them in
// the preview window
typedef struct {
kvec_t(SubResult) subresults;
linenr_T lines_needed; // lines needed in the preview window
} PreviewLines;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ex_cmds.c.generated.h"
#endif
static const char e_non_numeric_argument_to_z[]
= N_("E144: Non-numeric argument to :z");
/// ":ascii" and "ga" implementation
void do_ascii(exarg_T *eap)
{
char *data = get_cursor_pos_ptr();
size_t len = (size_t)utfc_ptr2len(data);
if (len == 0) {
msg("NUL", 0);
return;
}
bool need_clear = true;
msg_sb_eol();
msg_start();
int c = utf_ptr2char(data);
size_t off = 0;
// TODO(bfredl): merge this with the main loop
if (c < 0x80) {
if (c == NL) { // NUL is stored as NL.
c = NUL;
}
const int cval = (c == CAR && get_fileformat(curbuf) == EOL_MAC
? NL // NL is stored as CR.
: c);
char buf1[20];
if (vim_isprintc(c) && (c < ' ' || c > '~')) {
char buf3[7];
transchar_nonprint(curbuf, buf3, c);
vim_snprintf(buf1, sizeof(buf1), " <%s>", buf3);
} else {
buf1[0] = NUL;
}
char buf2[20];
buf2[0] = NUL;
char *dig = get_digraph_for_char(cval);
if (dig != NULL) {
vim_snprintf(IObuff, sizeof(IObuff),
_("<%s>%s%s %d, Hex %02x, Oct %03o, Digr %s"),
transchar(c), buf1, buf2, cval, cval, cval, dig);
} else {
vim_snprintf(IObuff, sizeof(IObuff),
_("<%s>%s%s %d, Hex %02x, Octal %03o"),
transchar(c), buf1, buf2, cval, cval, cval);
}
msg_multiline(IObuff, 0, true, &need_clear);
off += (size_t)utf_ptr2len(data); // needed for overlong ascii?
}
// Repeat for combining characters, also handle multiby here.
while (off < len) {
c = utf_ptr2char(data + off);
size_t iobuff_len = 0;
// This assumes every multi-byte char is printable...
if (off > 0) {
IObuff[iobuff_len++] = ' ';
}
IObuff[iobuff_len++] = '<';
if (utf_iscomposing(c)) {
IObuff[iobuff_len++] = ' '; // Draw composing char on top of a space.
}
iobuff_len += (size_t)utf_char2bytes(c, IObuff + iobuff_len);
char *dig = get_digraph_for_char(c);
if (dig != NULL) {
vim_snprintf(IObuff + iobuff_len, sizeof(IObuff) - iobuff_len,
(c < 0x10000
? _("> %d, Hex %04x, Oct %o, Digr %s")
: _("> %d, Hex %08x, Oct %o, Digr %s")),
c, c, c, dig);
} else {
vim_snprintf(IObuff + iobuff_len, sizeof(IObuff) - iobuff_len,
(c < 0x10000
? _("> %d, Hex %04x, Octal %o")
: _("> %d, Hex %08x, Octal %o")),
c, c, c);
}
msg_multiline(IObuff, 0, true, &need_clear);
off += (size_t)utf_ptr2len(data + off); // needed for overlong ascii?
}
if (need_clear) {
msg_clr_eos();
}
msg_end();
}
/// ":left", ":center" and ":right": align text.
void ex_align(exarg_T *eap)
{
int indent = 0;
int new_indent;
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;
}
}
int width = atoi(eap->arg);
pos_T 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 = (int)curbuf->b_p_tw;
}
if (width == 0 && curbuf->b_p_wm > 0) {
width = curwin->w_width_inner - (int)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 {
int has_tab = false; // avoid uninit warnings
int 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) {
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 {
set_indent(++new_indent, 0);
} while (linelen(NULL) <= width);
new_indent--;
break;
}
new_indent--;
}
}
}
}
if (new_indent < 0) {
new_indent = 0;
}
set_indent(new_indent, 0); // set indent
}
changed_lines(curbuf, eap->line1, 0, eap->line2 + 1, 0, true);
curwin->w_cursor = save_curpos;
beginline(BL_WHITE | BL_FIX);
}
/// @return the length of the current line, excluding trailing white space.
static int linelen(int *has_tab)
{
char *last;
// Get the line. If it's empty bail out early (could be the empty string
// for an unloaded buffer).
char *line = get_cursor_line_ptr();
if (*line == NUL) {
return 0;
}
// find the first non-blank character
char *first = skipwhite(line);
// find the character after the last non-blank character
for (last = first + strlen(first);
last > first && ascii_iswhite(last[-1]); last--) {}
char save = *last;
*last = NUL;
int len = linetabsize_str(line); // Get line length.
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 *sortbuf1;
static char *sortbuf2;
static bool sort_lc; ///< sort using locale
static bool sort_ic; ///< ignore case
static bool sort_nr; ///< sort on number
static bool sort_rx; ///< sort on regex instead of skipping it
static bool sort_flt; ///< sort on floating number
static bool 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
bool 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) FUNC_ATTR_NONNULL_ALL
{
if (sort_lc) {
return strcoll((const char *)s1, (const 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;
}
// When sorting numbers "start_col_nr" is the number, not the column
// number.
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 ? 1 : -1;
} 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.
memcpy(sortbuf1, ml_get(l1.lnum) + l1.st_u.line.start_col_nr,
(size_t)(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] = NUL;
memcpy(sortbuf2, ml_get(l2.lnum) + l2.st_u.line.start_col_nr,
(size_t)(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] = NUL;
result = string_compare(sortbuf1, sortbuf2);
}
// If two lines have the same value, preserve the original line order.
if (result == 0) {
return l1.lnum - l2.lnum;
}
return result;
}
/// ":sort".
void ex_sort(exarg_T *eap)
{
regmatch_T regmatch;
int maxlen = 0;
size_t count = (size_t)(eap->line2 - eap->line1) + 1;
size_t i;
bool unique = false;
int sort_what = 0;
// 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;
sorti_T *nrs = xmalloc(count * sizeof(sorti_T));
sort_abort = sort_ic = sort_lc = sort_rx = sort_nr = sort_flt = false;
size_t format_found = 0;
bool change_occurred = false; // Buffer contents changed.
for (char *p = eap->arg; *p != NUL; p++) {
if (ascii_iswhite(*p)) {
// Skip
} 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 = true;
format_found++;
} else if (*p == 'f') {
sort_flt = true;
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 (check_nextcmd(p) != NULL) {
eap->nextcmd = check_nextcmd(p);
break;
} else if (!ASCII_ISALPHA(*p) && regmatch.regprog == NULL) {
char *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_noprevre));
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_invarg2), p);
goto sortend;
}
}
// Can only have one of 'n', 'b', 'o' and 'x'.
if (format_found > 1) {
emsg(_(e_invarg));
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 (linenr_T lnum = eap->line1; lnum <= eap->line2; lnum++) {
char *s = ml_get(lnum);
int len = ml_get_len(lnum);
if (maxlen < len) {
maxlen = len;
}
colnr_T start_col = 0;
colnr_T 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
char *s2 = s + end_col;
char c = *s2; // temporary character storage
*s2 = NUL;
// Sorting on number: Store the number itself.
char *p = s + start_col;
if (sort_nr) {
if (sort_what & STR2NR_HEX) {
s = skiptohex(p);
} else if (sort_what & STR2NR_BIN) {
s = (char *)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(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 = xmalloc((size_t)maxlen + 1);
sortbuf2 = xmalloc((size_t)maxlen + 1);
// 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;
}
bcount_t old_count = 0;
bcount_t new_count = 0;
// Insert the lines in the sorted order below the last one.
linenr_T lnum = eap->line2;
for (i = 0; i < count; i++) {
const 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;
}
char *s = ml_get(get_lnum);
colnr_T bytelen = ml_get_len(get_lnum) + 1; // include EOL in bytelen
old_count += bytelen;
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, 0, false) == FAIL) {
break;
}
new_count += bytelen;
}
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, false);
}
} else {
count = 0;
}
// Adjust marks for deleted (or added) lines and prepare for displaying.
linenr_T deleted = (linenr_T)count - (lnum - eap->line2);
if (deleted > 0) {
mark_adjust(eap->line2 - deleted, eap->line2, MAXLNUM, -deleted, kExtmarkNOOP);
msgmore(-deleted);
} else if (deleted < 0) {
mark_adjust(eap->line2, MAXLNUM, -deleted, 0, kExtmarkNOOP);
}
if (change_occurred || deleted != 0) {
extmark_splice(curbuf, eap->line1 - 1, 0,
(int)count, 0, old_count,
lnum - eap->line2, 0, new_count, kExtmarkUndo);
changed_lines(curbuf, eap->line1, 0, eap->line2 + 1, -deleted, true);
}
curwin->w_cursor.lnum = eap->line1;
beginline(BL_WHITE | BL_FIX);
sortend:
xfree(nrs);
xfree(sortbuf1);
xfree(sortbuf2);
vim_regfree(regmatch.regprog);
if (got_int) {
emsg(_(e_interr));
}
}
/// :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)
{
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
if (dest >= line1 && dest < line2) {
emsg(_("E134: Cannot move a 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;
}
bcount_t start_byte = ml_find_line_or_offset(curbuf, line1, NULL, true);
bcount_t end_byte = ml_find_line_or_offset(curbuf, line2 + 1, NULL, true);
bcount_t extent_byte = end_byte - start_byte;
bcount_t dest_byte = ml_find_line_or_offset(curbuf, dest + 1, NULL, true);
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++) {
char *str = xstrnsave(ml_get(l + extra), (size_t)ml_get_len(l + extra));
ml_append(dest + l - line1, str, 0, false);
xfree(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, 0, kExtmarkNOOP);
disable_fold_update++;
changed_lines(curbuf, last_line - num_lines + 1, 0, last_line + 1, num_lines, false);
disable_fold_update--;
int line_off = 0;
bcount_t byte_off = 0;
if (dest >= line2) {
mark_adjust_nofold(line2 + 1, dest, -num_lines, 0, kExtmarkNOOP);
FOR_ALL_TAB_WINDOWS(tab, win) {
if (win->w_buffer == curbuf) {
foldMoveRange(win, &win->w_folds, line1, line2, dest);
}
}
if ((cmdmod.cmod_flags & CMOD_LOCKMARKS) == 0) {
curbuf->b_op_start.lnum = dest - num_lines + 1;
curbuf->b_op_end.lnum = dest;
}
line_off = -num_lines;
byte_off = -extent_byte;
} else {
mark_adjust_nofold(dest + 1, line1 - 1, num_lines, 0, kExtmarkNOOP);
FOR_ALL_TAB_WINDOWS(tab, win) {
if (win->w_buffer == curbuf) {
foldMoveRange(win, &win->w_folds, dest + 1, line1 - 1, line2);
}
}
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), 0, kExtmarkNOOP);
disable_fold_update++;
changed_lines(curbuf, last_line - num_lines + 1, 0, last_line + 1, -extra, false);
disable_fold_update--;
// send update regarding the new lines that were added
buf_updates_send_changes(curbuf, dest + 1, num_lines, 0);
// 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(line1 + extra, true);
}
if (!global_busy && num_lines > p_report) {
smsg(0, NGETTEXT("%" PRId64 " line moved",
"%" PRId64 " lines moved", num_lines),
(int64_t)num_lines);
}
extmark_move_region(curbuf, line1 - 1, 0, start_byte,
line2 - line1 + 1, 0, extent_byte,
dest + line_off, 0, dest_byte + byte_off,
kExtmarkUndo);
// 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(curbuf, line1, 0, dest, 0, false);
} else {
changed_lines(curbuf, dest + 1, 0, line1 + num_lines, 0, false);
}
// send nvim_buf_lines_event regarding lines that were deleted
buf_updates_send_changes(curbuf, line1 + extra, 0, num_lines);
return OK;
}
/// ":copy"
void ex_copy(linenr_T line1, linenr_T line2, linenr_T n)
{
linenr_T 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 make a copy because the line will be unlocked within ml_append()
char *p = xstrnsave(ml_get(line1), (size_t)ml_get_len(line1));
ml_append(curwin->w_cursor.lnum, p, 0, false);
xfree(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(count);
}
static char *prevcmd = NULL; // the previous command
#if defined(EXITFREE)
void free_prev_shellcmd(void)
{
xfree(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_noprev));
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, bool forceit, bool do_in, bool do_out)
FUNC_ATTR_NONNULL_ALL
{
char *arg = eap->arg; // command
linenr_T line1 = eap->line1; // start of range
linenr_T line2 = eap->line2; // end of range
char *newcmd = NULL; // the new command
bool free_newcmd = false; // need to free() newcmd
int scroll_save = msg_scroll;
// Disallow shell commands in secure mode
if (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.
bool ins_prevcmd = forceit;
// Skip leading white space to avoid a strange error with some shells.
char *trailarg = skipwhite(arg);
do {
size_t len = strlen(trailarg) + 1;
if (newcmd != NULL) {
len += strlen(newcmd);
}
if (ins_prevcmd) {
if (!prevcmd_is_set()) {
xfree(newcmd);
return;
}
len += strlen(prevcmd);
}
char *t = xmalloc(len);
*t = NUL;
if (newcmd != NULL) {
STRCAT(t, newcmd);
}
if (ins_prevcmd) {
STRCAT(t, prevcmd);
}
char *p = t + strlen(t);
STRCAT(t, trailarg);
xfree(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