aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-04-25 11:54:24 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2021-04-28 11:29:33 +0200
commitdadd90974639c0b8cb0561eb946746ca2678b5fb (patch)
tree711ff6b44c1352d22f23321564dcd27eb35b6e2a
parente6bc8a29a9f5e679f5c2f8e1ab3541dc99752350 (diff)
downloadbusybox-w32-dadd90974639c0b8cb0561eb946746ca2678b5fb.tar.gz
busybox-w32-dadd90974639c0b8cb0561eb946746ca2678b5fb.tar.bz2
busybox-w32-dadd90974639c0b8cb0561eb946746ca2678b5fb.zip
vi: improvements to ':read' command
Improvements to ':read': - When a file is read into the current buffer the cursor should be placed on the first line read. - If invoked without supplying a filename the current filename should be used. This is similar to how ':edit' works. - The code for ':edit' included an explicit check that the current filename was non-empty. Both vim and traditional vi accept non-empty filenames, only issuing an error message when an attempt to use such a name fails. - Allow undo of a file read. function old new delta file_insert 367 382 +15 colon 3841 3848 +7 .rodata 105236 105218 -18 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/1 up/down: 22/-18) Total: 4 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/vi.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/editors/vi.c b/editors/vi.c
index 8d9d04a88..dd22eb45b 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2007,6 +2007,11 @@ static int file_insert(const char *fn, char *p, int initial)
2007 p = text_hole_delete(p + cnt, p + size - 1, NO_UNDO); 2007 p = text_hole_delete(p + cnt, p + size - 1, NO_UNDO);
2008 status_line_bold("can't read '%s'", fn); 2008 status_line_bold("can't read '%s'", fn);
2009 } 2009 }
2010# if ENABLE_FEATURE_VI_UNDO
2011 else {
2012 undo_push_insert(p, size, ALLOW_UNDO);
2013 }
2014# endif
2010 fi: 2015 fi:
2011 close(fd); 2016 close(fd);
2012 2017
@@ -2743,10 +2748,7 @@ static void colon(char *buf)
2743 if (args[0]) { 2748 if (args[0]) {
2744 // the user supplied a file name 2749 // the user supplied a file name
2745 fn = args; 2750 fn = args;
2746 } else if (current_filename && current_filename[0]) { 2751 } else if (current_filename == NULL) {
2747 // no user supplied name- use the current filename
2748 // fn = current_filename; was set by default
2749 } else {
2750 // no user file name, no current name- punt 2752 // no user file name, no current name- punt
2751 status_line_bold("No current filename"); 2753 status_line_bold("No current filename");
2752 goto ret; 2754 goto ret;
@@ -2864,11 +2866,14 @@ static void colon(char *buf)
2864 } 2866 }
2865 editing = 0; 2867 editing = 0;
2866 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[] 2868 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
2867 int size; 2869 int size, num;
2868 2870
2869 fn = args; 2871 if (args[0]) {
2870 if (!fn[0]) { 2872 // the user supplied a file name
2871 status_line_bold("No filename given"); 2873 fn = args;
2874 } else if (current_filename == NULL) {
2875 // no user file name, no current name- punt
2876 status_line_bold("No current filename");
2872 goto ret; 2877 goto ret;
2873 } 2878 }
2874 if (e < 0) { // no addr given- read after current line 2879 if (e < 0) { // no addr given- read after current line
@@ -2881,6 +2886,9 @@ static void colon(char *buf)
2881 if (q == end-1) 2886 if (q == end-1)
2882 ++q; 2887 ++q;
2883 } 2888 }
2889 num = count_lines(text, q);
2890 if (q == end)
2891 num++;
2884 { // dance around potentially-reallocated text[] 2892 { // dance around potentially-reallocated text[]
2885 uintptr_t ofs = q - text; 2893 uintptr_t ofs = q - text;
2886 size = file_insert(fn, q, 0); 2894 size = file_insert(fn, q, 0);
@@ -2897,11 +2905,7 @@ static void colon(char *buf)
2897 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),) 2905 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
2898 li, size 2906 li, size
2899 ); 2907 );
2900 if (size > 0) { 2908 dot = find_line(num);
2901 // if the insert is before "dot" then we need to update
2902 if (q <= dot)
2903 dot += size;
2904 }
2905 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args 2909 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
2906 if (modified_count && !useforce) { 2910 if (modified_count && !useforce) {
2907 status_line_bold("No write since last change (:%s! overrides)", cmd); 2911 status_line_bold("No write since last change (:%s! overrides)", cmd);