vi: don't touch file with :x when modified_count == 0

Along with it, there are other changes

 - Check for uppercase X is removed as the expression will be always false and
   :X itself is another totally different command in standard vim
 - The status line will show number of written lines instead of lines requested
   by the colon command.  This is also how the standard vim is doing, though
   the difference is that '!' has to be explicitly specified in vim to allow
   partial writes

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
1_27_stable
Denys Vlasenko 2017-03-13 20:50:42 +01:00
parent 02a2a278f6
commit e88608eae2
1 changed files with 26 additions and 17 deletions

View File

@ -1034,7 +1034,9 @@ static void colon(char *buf)
|| strncmp(p, "wn", cnt) == 0 || strncmp(p, "wn", cnt) == 0
|| (p[0] == 'x' && !p[1]) || (p[0] == 'x' && !p[1])
) { ) {
if (modified_count != 0 || p[0] != 'x') {
cnt = file_write(current_filename, text, end - 1); cnt = file_write(current_filename, text, end - 1);
}
if (cnt < 0) { if (cnt < 0) {
if (cnt == -1) if (cnt == -1)
status_line_bold("Write error: %s", strerror(errno)); status_line_bold("Write error: %s", strerror(errno));
@ -1045,8 +1047,9 @@ static void colon(char *buf)
current_filename, current_filename,
count_lines(text, end - 1), cnt count_lines(text, end - 1), cnt
); );
if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n' if (p[0] == 'x'
|| p[0] == 'X' || p[1] == 'Q' || p[1] == 'N' || p[1] == 'q' || p[1] == 'n'
|| p[1] == 'Q' || p[1] == 'N'
) { ) {
editing = 0; editing = 0;
} }
@ -1476,16 +1479,19 @@ static void colon(char *buf)
goto ret; goto ret;
} }
#endif #endif
// how many lines in text[]?
li = count_lines(q, r);
size = r - q + 1;
//if (useforce) { //if (useforce) {
// if "fn" is not write-able, chmod u+w // if "fn" is not write-able, chmod u+w
// sprintf(syscmd, "chmod u+w %s", fn); // sprintf(syscmd, "chmod u+w %s", fn);
// system(syscmd); // system(syscmd);
// forced = TRUE; // forced = TRUE;
//} //}
if (modified_count != 0 || cmd[0] != 'x') {
size = r - q + 1;
l = file_write(fn, q, r); l = file_write(fn, q, r);
} else {
size = 0;
l = 0;
}
//if (useforce && forced) { //if (useforce && forced) {
// chmod u-w // chmod u-w
// sprintf(syscmd, "chmod u-w %s", fn); // sprintf(syscmd, "chmod u-w %s", fn);
@ -1496,19 +1502,22 @@ static void colon(char *buf)
if (l == -1) if (l == -1)
status_line_bold_errno(fn); status_line_bold_errno(fn);
} else { } else {
// how many lines written
li = count_lines(q, q + l - 1);
status_line("'%s' %dL, %dC", fn, li, l); status_line("'%s' %dL, %dC", fn, li, l);
if (q == text && r == end - 1 && l == size) { if (l == size) {
if (q == text && q + l == end) {
modified_count = 0; modified_count = 0;
last_modified_count = -1; last_modified_count = -1;
} }
if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' if (cmd[0] == 'x'
|| cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N' || cmd[1] == 'q' || cmd[1] == 'n'
) || cmd[1] == 'Q' || cmd[1] == 'N'
&& l == size
) { ) {
editing = 0; editing = 0;
} }
} }
}
#if ENABLE_FEATURE_VI_YANKMARK #if ENABLE_FEATURE_VI_YANKMARK
} else if (strncmp(cmd, "yank", i) == 0) { // yank lines } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
if (b < 0) { // no addr given- use defaults if (b < 0) { // no addr given- use defaults