Commit 4d166e4f13522f46fd1b754687f4785d9f5fe34b

antirez 2013-02-06T12:28:29

Backspace functionality refactored into linenoiseEditBackspace() function.

diff --git a/linenoise.c b/linenoise.c
index 8681637..896070c 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -416,6 +416,17 @@ void linenoiseEditDelete(struct linenoiseState *l) {
     }
 }
 
+/* Backspace implementation. */
+void linenoiseEditBackspace(struct linenoiseState *l) {
+    if (l->pos > 0 && l->len > 0) {
+        memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);
+        l->pos--;
+        l->len--;
+        l->buf[l->len] = '\0';
+        refreshLine(l);
+    }
+}
+
 /* This function is the core of the line editing capability of linenoise.
  * It expects 'fd' to be already in "raw mode" so that every key pressed
  * will be returned ASAP to read().
@@ -480,13 +491,7 @@ static int linenoiseEdit(int fd, char *buf, size_t buflen, const char *prompt)
             return -1;
         case 127:   /* backspace */
         case 8:     /* ctrl-h */
-            if (l.pos > 0 && l.len > 0) {
-                memmove(buf+l.pos-1,buf+l.pos,l.len - l.pos);
-                l.pos--;
-                l.len--;
-                buf[l.len] = '\0';
-                refreshLine(&l);
-            }
+            linenoiseEditBackspace(&l);
             break;
         case 4:     /* ctrl-d, remove char at right of cursor, or of the
                        line is empty, act as end-of-file. */