diff options
author | Ron Yorston <rmy@pobox.com> | 2021-04-17 09:25:47 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-04-20 11:21:43 +0200 |
commit | f277c9eebb91a46cbd795c34aa64ee8b6a2e448c (patch) | |
tree | 184512a390ddbe64607d2bd60fbcd26b5aa1b083 | |
parent | 310ef232809b34a75c120629b7b2c0d4af0dd50f (diff) | |
download | busybox-w32-f277c9eebb91a46cbd795c34aa64ee8b6a2e448c.tar.gz busybox-w32-f277c9eebb91a46cbd795c34aa64ee8b6a2e448c.tar.bz2 busybox-w32-f277c9eebb91a46cbd795c34aa64ee8b6a2e448c.zip |
vi: make de-indentation with ctrl-D more like vim
Commit ac6495f6f (vi: allow ctrl-D to reduce indentation) treated
ctrl-D during autoindent as a backspace. This was adequate for
indentation using tabs but doesn't work well with the expandtab
option. In the latter case it's necessary to backspace over all
the spaces.
Make ctrl-D work correctly when spaces are present in the indent.
Also, make it behave more like vim:
- ctrl-D is independent of autoindent;
- indentation is reduced even when the cursor isn't positioned at
the end of the indent.
function old new delta
char_insert 679 717 +38
get_column - 37 +37
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 75/0) Total: 75 bytes
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/vi.c | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/editors/vi.c b/editors/vi.c index de7a43325..caf01acd3 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -763,6 +763,11 @@ static int next_tabstop(int col) | |||
763 | return col + ((tabstop - 1) - (col % tabstop)); | 763 | return col + ((tabstop - 1) - (col % tabstop)); |
764 | } | 764 | } |
765 | 765 | ||
766 | static int prev_tabstop(int col) | ||
767 | { | ||
768 | return col - ((col % tabstop) ?: tabstop); | ||
769 | } | ||
770 | |||
766 | static int next_column(char c, int co) | 771 | static int next_column(char c, int co) |
767 | { | 772 | { |
768 | if (c == '\t') | 773 | if (c == '\t') |
@@ -772,7 +777,6 @@ static int next_column(char c, int co) | |||
772 | return co + 1; | 777 | return co + 1; |
773 | } | 778 | } |
774 | 779 | ||
775 | #if ENABLE_FEATURE_VI_SETOPTS | ||
776 | static int get_column(char *p) | 780 | static int get_column(char *p) |
777 | { | 781 | { |
778 | const char *r; | 782 | const char *r; |
@@ -782,7 +786,6 @@ static int get_column(char *p) | |||
782 | co = next_column(*r, co); | 786 | co = next_column(*r, co); |
783 | return co; | 787 | return co; |
784 | } | 788 | } |
785 | #endif | ||
786 | 789 | ||
787 | //----- Erase the Screen[] memory ------------------------------ | 790 | //----- Erase the Screen[] memory ------------------------------ |
788 | static void screen_erase(void) | 791 | static void screen_erase(void) |
@@ -2113,14 +2116,23 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p' | |||
2113 | if ((p[-1] != '\n') && (dot > text)) { | 2116 | if ((p[-1] != '\n') && (dot > text)) { |
2114 | p--; | 2117 | p--; |
2115 | } | 2118 | } |
2116 | #if ENABLE_FEATURE_VI_SETOPTS | 2119 | } else if (c == 4) { // ctrl-D reduces indentation |
2117 | } else if (c == 4 && autoindent) { // ctrl-D reduces indentation | 2120 | int prev; |
2118 | q = begin_line(p); | 2121 | char *r, *bol; |
2119 | len = strspn(q, " \t"); | 2122 | bol = begin_line(p); |
2120 | if (len && q + len == p) { | 2123 | for (r = bol; r < end_line(p); ++r) { |
2121 | p--; | 2124 | if (!isblank(*r)) |
2122 | p = text_hole_delete(p, p, ALLOW_UNDO_QUEUED); | 2125 | break; |
2123 | } | 2126 | } |
2127 | |||
2128 | prev = prev_tabstop(get_column(r)); | ||
2129 | while (r > bol && get_column(r) > prev) { | ||
2130 | if (p > bol) | ||
2131 | p--; | ||
2132 | r--; | ||
2133 | r = text_hole_delete(r, r, ALLOW_UNDO_QUEUED); | ||
2134 | } | ||
2135 | #if ENABLE_FEATURE_VI_SETOPTS | ||
2124 | } else if (c == '\t' && expandtab) { // expand tab | 2136 | } else if (c == '\t' && expandtab) { // expand tab |
2125 | int col = get_column(p); | 2137 | int col = get_column(p); |
2126 | col = next_tabstop(col) - col + 1; | 2138 | col = next_tabstop(col) - col + 1; |