Commit c56aa8d12c4ae0b09627e5c17e438b4ac3f12b5d

antirez 2010-03-22T19:49:56

supported more emacs bindings, don't wrap history forever but blocks on first and last item

diff --git a/linenoise.c b/linenoise.c
index bcef68e..8267ce5 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -209,14 +209,14 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
         case 13:    /* enter */
             history_len--;
             return len;
-        case 4:     /* ctrl+d */
+        case 4:     /* ctrl-d */
             history_len--;
             return (len == 0) ? -1 : (int)len;
-        case 3:     /* ctrl+c */
+        case 3:     /* ctrl-c */
             errno = EAGAIN;
             return -1;
         case 127:   /* backspace */
-        case 8:     /* ctrl+h */
+        case 8:     /* ctrl-h */
             if (pos > 0 && len > 0) {
                 memmove(buf+pos-1,buf+pos,len-pos);
                 pos--;
@@ -225,21 +225,35 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
                 refreshLine(fd,prompt,buf,len,pos,cols);
             }
             break;
+        case 2:     /* ctrl-b */
+            goto left_arrow;
+        case 6:     /* ctrl-f */
+            goto right_arrow;
+        case 16:    /* ctrl-p */
+            seq[1] = 65;
+            goto up_down_arrow;
+        case 14:    /* ctrl-n */
+            seq[1] = 66;
+            goto up_down_arrow;
+            break;
         case 27:    /* escape sequence */
             if (read(fd,seq,2) == -1) break;
             if (seq[0] == 91 && seq[1] == 68) {
+left_arrow:
                 /* left arrow */
                 if (pos > 0) {
                     pos--;
                     refreshLine(fd,prompt,buf,len,pos,cols);
                 }
             } else if (seq[0] == 91 && seq[1] == 67) {
+right_arrow:
                 /* right arrow */
                 if (pos != len) {
                     pos++;
                     refreshLine(fd,prompt,buf,len,pos,cols);
                 }
             } else if (seq[0] == 91 && (seq[1] == 65 || seq[1] == 66)) {
+up_down_arrow:
                 /* up and down arrow: history */
                 if (history_len > 1) {
                     /* Update the current history entry before to
@@ -248,10 +262,13 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
                     history[history_len-1-history_index] = strdup(buf);
                     /* Show the new entry */
                     history_index += (seq[1] == 65) ? 1 : -1;
-                    if (history_index < 0)
-                        history_index = history_len-1;
-                    else if (history_index >= history_len)
+                    if (history_index < 0) {
                         history_index = 0;
+                        break;
+                    } else if (history_index >= history_len) {
+                        history_index = history_len-1;
+                        break;
+                    }
                     strncpy(buf,history[history_len-1-history_index],buflen);
                     buf[buflen] = '\0';
                     len = pos = strlen(buf);