[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 06/12] vi: allow ctrl-D to reduce indentation
From:       Ron Yorston <rmy () pobox ! com>
Date:       2021-04-15 11:03:58
Message-ID: 60781d9e.qXkICcBfADs9bwzO%rmy () pobox ! com
[Download RAW message or body]

When whitespace has been automatically added to a new line due to
autoindent entering ctrl-D should reduce the level of indentation.

Implement an approximation of this by treating ctrl-D as backspace.
For the common case of indentation using tabs this is good enough.
My attempt at a full implementation was three times bigger.

function                                             old     new   delta
char_insert                                          476     531     +55
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 55/0)               Total: 55 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 editors/vi.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index 780c81234..0a82f9e38 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2072,6 +2072,11 @@ static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at
 #endif
 static char *char_insert(char *p, char c, int undo) // insert the char c at 'p'
 {
+#if ENABLE_FEATURE_VI_SETOPTS
+		char *q;
+		size_t len;
+#endif
+
 	if (c == 22) {		// Is this an ctrl-V?
 		p += stupid_insert(p, '^');	// use ^ to indicate literal next
 		refresh(FALSE);	// show the ^
@@ -2092,6 +2097,15 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p'
 		if ((p[-1] != '\n') && (dot > text)) {
 			p--;
 		}
+#if ENABLE_FEATURE_VI_SETOPTS
+	} else if (c == 4 && autoindent) {	// ctrl-D reduces indentation
+		q = begin_line(p);
+		len = strspn(q, " \t");
+		if (len && q + len == p) {
+			p--;
+			p = text_hole_delete(p, p, ALLOW_UNDO_QUEUED);
+		}
+#endif
 	} else if (c == term_orig.c_cc[VERASE] || c == 8 || c == 127) { // Is this a BS
 		if (p > text) {
 			p--;
@@ -2116,8 +2130,6 @@ static char *char_insert(char *p, char c, int undo) // insert the char c at 'p'
 			showmatching(p - 1);
 		}
 		if (autoindent && c == '\n') {	// auto indent the new line
-			char *q;
-			size_t len;
 			q = prev_line(p);	// use prev line as template
 			len = strspn(q, " \t"); // space or tab
 			if (len) {
-- 
2.30.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic