401-vi-don-t-touch-file-with-x-when-modified_count-0.patch 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. From e88608eae24ae5934034e1ecb6c494fefbf1b9ae Mon Sep 17 00:00:00 2001
  2. From: Denys Vlasenko <vda.linux@googlemail.com>
  3. Date: Mon, 13 Mar 2017 20:50:42 +0100
  4. Subject: [PATCH 1/2] vi: don't touch file with :x when modified_count == 0
  5. Along with it, there are other changes
  6. - Check for uppercase X is removed as the expression will be always false and
  7. :X itself is another totally different command in standard vim
  8. - The status line will show number of written lines instead of lines requested
  9. by the colon command. This is also how the standard vim is doing, though
  10. the difference is that '!' has to be explicitly specified in vim to allow
  11. partial writes
  12. Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
  13. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
  14. ---
  15. editors/vi.c | 43 ++++++++++++++++++++++++++-----------------
  16. 1 file changed, 26 insertions(+), 17 deletions(-)
  17. --- a/editors/vi.c
  18. +++ b/editors/vi.c
  19. @@ -1038,7 +1038,9 @@ static void colon(char *buf)
  20. || strncmp(p, "wn", cnt) == 0
  21. || (p[0] == 'x' && !p[1])
  22. ) {
  23. - cnt = file_write(current_filename, text, end - 1);
  24. + if (modified_count != 0 || p[0] != 'x') {
  25. + cnt = file_write(current_filename, text, end - 1);
  26. + }
  27. if (cnt < 0) {
  28. if (cnt == -1)
  29. status_line_bold("Write error: %s", strerror(errno));
  30. @@ -1049,8 +1051,9 @@ static void colon(char *buf)
  31. current_filename,
  32. count_lines(text, end - 1), cnt
  33. );
  34. - if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
  35. - || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
  36. + if (p[0] == 'x'
  37. + || p[1] == 'q' || p[1] == 'n'
  38. + || p[1] == 'Q' || p[1] == 'N'
  39. ) {
  40. editing = 0;
  41. }
  42. @@ -1480,16 +1483,19 @@ static void colon(char *buf)
  43. goto ret;
  44. }
  45. #endif
  46. - // how many lines in text[]?
  47. - li = count_lines(q, r);
  48. - size = r - q + 1;
  49. //if (useforce) {
  50. // if "fn" is not write-able, chmod u+w
  51. // sprintf(syscmd, "chmod u+w %s", fn);
  52. // system(syscmd);
  53. // forced = TRUE;
  54. //}
  55. - l = file_write(fn, q, r);
  56. + if (modified_count != 0 || cmd[0] != 'x') {
  57. + size = r - q + 1;
  58. + l = file_write(fn, q, r);
  59. + } else {
  60. + size = 0;
  61. + l = 0;
  62. + }
  63. //if (useforce && forced) {
  64. // chmod u-w
  65. // sprintf(syscmd, "chmod u-w %s", fn);
  66. @@ -1500,17 +1506,20 @@ static void colon(char *buf)
  67. if (l == -1)
  68. status_line_bold_errno(fn);
  69. } else {
  70. + // how many lines written
  71. + li = count_lines(q, q + l - 1);
  72. status_line("'%s' %dL, %dC", fn, li, l);
  73. - if (q == text && r == end - 1 && l == size) {
  74. - modified_count = 0;
  75. - last_modified_count = -1;
  76. - }
  77. - if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
  78. - || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
  79. - )
  80. - && l == size
  81. - ) {
  82. - editing = 0;
  83. + if (l == size) {
  84. + if (q == text && q + l == end) {
  85. + modified_count = 0;
  86. + last_modified_count = -1;
  87. + }
  88. + if (cmd[0] == 'x'
  89. + || cmd[1] == 'q' || cmd[1] == 'n'
  90. + || cmd[1] == 'Q' || cmd[1] == 'N'
  91. + ) {
  92. + editing = 0;
  93. + }
  94. }
  95. }
  96. #if ENABLE_FEATURE_VI_YANKMARK