diff options
| author | Ron Yorston <rmy@pobox.com> | 2021-05-20 08:27:19 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-02 06:16:36 +0200 |
| commit | 16e2fa9049d5ddd7d4c8aea875dad91e07868685 (patch) | |
| tree | db0cd0f8737feeff6663e0feaf4f4c72613e7919 | |
| parent | d95f89ec576c5a0ecba24ead7f012f1fd8ea7b9b (diff) | |
| download | busybox-w32-16e2fa9049d5ddd7d4c8aea875dad91e07868685.tar.gz busybox-w32-16e2fa9049d5ddd7d4c8aea875dad91e07868685.tar.bz2 busybox-w32-16e2fa9049d5ddd7d4c8aea875dad91e07868685.zip | |
vi: make autoindent respect expandtab setting
Autoindent took a copy of the indent from a neighbouring line, which
may not have respected the expandtab setting.
Determine the target column and construct a suitable indent. This
will consist entirely of spaces if expandtab is enabled or an
efficient combination of tabs and spaces otherwise.
function old new delta
char_insert 719 741 +22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 22/0) Total: 22 bytes
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | editors/vi.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/editors/vi.c b/editors/vi.c index c4f3b9660..36116e677 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
| @@ -2111,6 +2111,7 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p' | |||
| 2111 | #if ENABLE_FEATURE_VI_SETOPTS | 2111 | #if ENABLE_FEATURE_VI_SETOPTS |
| 2112 | char *q; | 2112 | char *q; |
| 2113 | size_t len; | 2113 | size_t len; |
| 2114 | int col, ntab, nspc; | ||
| 2114 | #endif | 2115 | #endif |
| 2115 | 2116 | ||
| 2116 | if (c == 22) { // Is this an ctrl-V? | 2117 | if (c == 22) { // Is this an ctrl-V? |
| @@ -2151,7 +2152,7 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p' | |||
| 2151 | } | 2152 | } |
| 2152 | #if ENABLE_FEATURE_VI_SETOPTS | 2153 | #if ENABLE_FEATURE_VI_SETOPTS |
| 2153 | } else if (c == '\t' && expandtab) { // expand tab | 2154 | } else if (c == '\t' && expandtab) { // expand tab |
| 2154 | int col = get_column(p); | 2155 | col = get_column(p); |
| 2155 | col = next_tabstop(col) - col + 1; | 2156 | col = next_tabstop(col) - col + 1; |
| 2156 | while (col--) { | 2157 | while (col--) { |
| 2157 | # if ENABLE_FEATURE_VI_UNDO | 2158 | # if ENABLE_FEATURE_VI_UNDO |
| @@ -2186,23 +2187,28 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p' | |||
| 2186 | showmatching(p - 1); | 2187 | showmatching(p - 1); |
| 2187 | } | 2188 | } |
| 2188 | if (autoindent && c == '\n') { // auto indent the new line | 2189 | if (autoindent && c == '\n') { // auto indent the new line |
| 2189 | // use current/previous line as template | 2190 | // use indent of current/previous line |
| 2190 | q = openabove ? p : prev_line(p); | 2191 | q = openabove ? p : prev_line(p); |
| 2191 | len = strspn(q, " \t"); // space or tab | 2192 | len = strspn(q, " \t"); // space or tab |
| 2192 | if (openabove) { | 2193 | if (openabove) |
| 2193 | p--; // this replaces dot_prev() in do_cmd() | 2194 | p--; // indent goes before newly inserted NL |
| 2194 | q += len; // template will be shifted by text_hole_make() | ||
| 2195 | } | ||
| 2196 | if (len) { | 2195 | if (len) { |
| 2197 | uintptr_t bias; | 2196 | col = get_column(q + len); |
| 2198 | bias = text_hole_make(p, len); | 2197 | if (expandtab) { |
| 2199 | p += bias; | 2198 | ntab = 0; |
| 2200 | q += bias; | 2199 | nspc = col; |
| 2200 | } else { | ||
| 2201 | ntab = col / tabstop; | ||
| 2202 | nspc = col % tabstop; | ||
| 2203 | } | ||
| 2204 | p += text_hole_make(p, ntab + nspc); | ||
| 2201 | # if ENABLE_FEATURE_VI_UNDO | 2205 | # if ENABLE_FEATURE_VI_UNDO |
| 2202 | undo_push_insert(p, len, undo); | 2206 | undo_push_insert(p, ntab + nspc, undo); |
| 2203 | # endif | 2207 | # endif |
| 2204 | memcpy(p, q, len); | 2208 | memset(p, '\t', ntab); |
| 2205 | p += len; | 2209 | p += ntab; |
| 2210 | memset(p, ' ', nspc); | ||
| 2211 | p += nspc; | ||
| 2206 | } | 2212 | } |
| 2207 | } | 2213 | } |
| 2208 | #endif | 2214 | #endif |
