]> git.lizzy.rs Git - linenoise.git/blobdiff - linenoise.c
Use CMake
[linenoise.git] / linenoise.c
index 91451f454dcf6517dfcb090f25748634de0675bd..4300cb8677c5c03b7fc084b6dddbacf813edaa71 100644 (file)
 #ifndef STRINGBUF_H
 #include "stringbuf.h"
 #endif
+#ifndef UTF8_UTIL_H
 #include "utf8.h"
+#endif
 
 #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
 
@@ -227,15 +229,17 @@ void linenoiseHistoryFree(void) {
     }
 }
 
+typedef enum {
+    EP_START,   /* looking for ESC */
+    EP_ESC,     /* looking for [ */
+    EP_DIGITS,  /* parsing digits */
+    EP_PROPS,   /* parsing digits or semicolons */
+    EP_END,     /* ok */
+    EP_ERROR,   /* error */
+} ep_state_t;
+
 struct esc_parser {
-    enum {
-        EP_START,   /* looking for ESC */
-        EP_ESC,     /* looking for [ */
-        EP_DIGITS,  /* parsing digits */
-        EP_PROPS,   /* parsing digits or semicolons */
-        EP_END,     /* ok */
-        EP_ERROR,   /* error */
-    } state;
+    ep_state_t state;
     int props[5];   /* properties are stored here */
     int maxprops;   /* size of the props[] array */
     int numprops;   /* number of properties found */
@@ -342,7 +346,7 @@ static void DRL_STR(const char *str)
     }
 }
 #else
-#define DRL(ARGS...)
+#define DRL(...)
 #define DRL_CHAR(ch)
 #define DRL_STR(str)
 #endif
@@ -1016,6 +1020,7 @@ static void refreshEnd(struct current *current)
 
 static void refreshStartChars(struct current *current)
 {
+    (void)current;
 }
 
 static void refreshNewline(struct current *current)
@@ -1026,6 +1031,7 @@ static void refreshNewline(struct current *current)
 
 static void refreshEndChars(struct current *current)
 {
+    (void)current;
 }
 #endif
 
@@ -1281,13 +1287,14 @@ static int remove_char(struct current *current, int pos)
         int rc = 1;
 
         /* Now we try to optimise in the simple but very common case that:
-         * - we are remove the char at EOL
+         * - outputChars() can be used directly (not win32)
+         * - we are removing the char at EOL
          * - the buffer is not empty
          * - there are columns available to the left
          * - the char being deleted is not a wide or utf-8 character
          * - no hints are being shown
          */
-        if (current->pos == pos + 1 && current->pos == sb_chars(current->buf) && pos > 0) {
+        if (current->output && current->pos == pos + 1 && current->pos == sb_chars(current->buf) && pos > 0) {
 #ifdef USE_UTF8
             /* Could implement utf8_prev_len() but simplest just to not optimise this case */
             char last = sb_str(current->buf)[offset];
@@ -1342,11 +1349,12 @@ static int insert_char(struct current *current, int pos, int ch)
         buf[n] = 0;
 
         /* Now we try to optimise in the simple but very common case that:
+         * - outputChars() can be used directly (not win32)
          * - we are inserting at EOL
          * - there are enough columns available
          * - no hints are being shown
          */
-        if (pos == current->pos && pos == sb_chars(current->buf)) {
+        if (current->output && pos == current->pos && pos == sb_chars(current->buf)) {
             int width = char_display_width(ch);
             if (current->colsright > width) {
                 /* Yes, can optimise */
@@ -1461,8 +1469,8 @@ static int reverseIncrementalSearch(struct current *current)
         c = fd_read(current);
         if (c == ctrl('H') || c == CHAR_DELETE) {
             if (rchars) {
-                int p = utf8_index(rbuf, --rchars);
-                rbuf[p] = 0;
+                int p_ind = utf8_index(rbuf, --rchars);
+                rbuf[p_ind] = 0;
                 rlen = strlen(rbuf);
             }
             continue;
@@ -1584,7 +1592,8 @@ static int linenoiseEdit(struct current *current) {
         switch(c) {
         case SPECIAL_NONE:
             break;
-        case '\r':    /* enter */
+        case '\r':    /* enter/CR */
+        case '\n':    /* LF */
             history_len--;
             free(history[history_len]);
             current->pos = sb_chars(current->buf);