]> git.mdlowis.com Git - projs/tide.git/commitdiff
enter key now inserts \r\n in crlf mode
authorMichael D. Lowis <mike@mdlowis.com>
Sun, 16 Oct 2016 02:51:17 +0000 (22:51 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sun, 16 Oct 2016 02:51:17 +0000 (22:51 -0400)
buf.c
charset.c
edit.h
keyboard.c

diff --git a/buf.c b/buf.c
index c7e8f49c1aa870e64b7bf246328937f9060d4f14..8e1d2175636ebb9e02d419d3af0a61a141e5a2de 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -14,7 +14,7 @@ void buf_load(Buf* buf, char* path) {
     } else {
         FMap file = fmap(path);
         buf->path = strdup(path);
-        buf->charset = (file.buf ? charset(file.buf, file.len) : UTF_8);
+        buf->charset = (file.buf ? charset(file.buf, file.len, &buf->crlf) : UTF_8);
         /* load the file contents if it has any */
         if (buf->charset > UTF_8) {
             die("Unsupported character set");
index e002f1f57485a4897e0b1065574feef4b88a0572..0900bc3c32560bcce5398d8ecbcc8ca59fa4411d 100644 (file)
--- a/charset.c
+++ b/charset.c
@@ -23,7 +23,9 @@ static const char Utf8Valid[256] = {
     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
 };
 
-int charset(const uint8_t* buf, size_t len) {
+int charset(const uint8_t* buf, size_t len, int* crlf) {
+    size_t crs = 0;
+    size_t lfs = 0;
     /* look for a BOM and parse it */
     for (size_t i = 0; i < (sizeof(BOMS)/sizeof(BOMS[0])); i++)
         if (!strncmp((char*)buf, BOMS[i].seq, BOMS[i].len))
@@ -31,8 +33,15 @@ int charset(const uint8_t* buf, size_t len) {
     /* look for bytes that are invalid in utf-8 */
     int type = UTF_8;
     size_t i = 0;
-    for (i = 0; type && (i < len); i++)
-        type = Utf8Valid[(int)buf[i]];
+    for (i = 0; i < len; i++) {
+        type &= Utf8Valid[(int)buf[i]];
+        switch(buf[i]) {
+            case '\r': crs++; break;
+            case '\n': lfs++; break;
+        }
+    }
+    /* report back the linefeed mode */
+    *crlf = (crs > (lfs / 2));
     return type;
 }
 
diff --git a/edit.h b/edit.h
index fe4830363fa53c4215597815c71b2e600b6389a2..2d34cc1130d7b5d362d5673f8ac71458ff98fa81 100644 (file)
--- a/edit.h
+++ b/edit.h
@@ -40,6 +40,7 @@ void fputrune(Rune rune, FILE* f);
 typedef struct buf {
     char* path;       /* the path to the open file */
     int charset;      /* the character set of the buffer */
+    int crlf;         /* tracks whether the file uses dos style line endings */
     bool insert_mode; /* tracks current mode */
     bool modified;    /* tracks whether the buffer has been modified */
     size_t bufsize;   /* size of the buffer in runes */
@@ -75,7 +76,7 @@ enum {
     UTF_32LE,   /* UTF-32 encoding, little-endian */
 };
 
-int charset(const uint8_t* buf, size_t len);
+int charset(const uint8_t* buf, size_t len, int* crlf);
 void utf8load(Buf* buf, FMap file);
 void utf8save(Buf* buf, FILE* file);
 void binload(Buf* buf, FMap file);
@@ -276,7 +277,7 @@ static const Color Palette[][2] = {
 #ifdef __MACH__
 #define FONTNAME "Monaco:size=10:antialias=true:autohint=true"
 #else
-#define FONTNAME "Monaco:size=10.5:antialias=true:autohint=true"
+#define FONTNAME "Liberation Mono:size=10.5:antialias=true:autohint=true"
 #endif
 
 #define DEFAULT_COLORSCHEME DARK
index 0a970fb40cc70101f4a175793392cdef4da58b81..d6d2b096ad0ed728bde2b368440060f6242ac5c7 100644 (file)
@@ -40,6 +40,12 @@ static void backspace(void) {
     TargetCol = buf_getcol(&Buffer, CursorPos);
 }
 
+static void insert(Rune r) {
+    if (r == '\n' && Buffer.crlf)
+        buf_ins(&Buffer, CursorPos++, '\r');
+    buf_ins(&Buffer, CursorPos++, r);
+}
+
 void handle_key(Rune key) {
     /* ignore invalid keys */
     if (key == RUNE_ERR) return;
@@ -73,11 +79,11 @@ static void special_keys(Rune key) {
 
 static void control_keys(Rune key) {
     switch (key) {
-        case KEY_ESCAPE:    Buffer.insert_mode = false;         break;
-        case KEY_BACKSPACE: backspace();                        break;
-        case KEY_CTRL_W:    buf_save(&Buffer);                  break;
-        case KEY_CTRL_Q:    exit(0);                            break;
-        default:            buf_ins(&Buffer, CursorPos++, key); break;
+        case KEY_ESCAPE:    Buffer.insert_mode = false; break;
+        case KEY_BACKSPACE: backspace();                break;
+        case KEY_CTRL_W:    buf_save(&Buffer);          break;
+        case KEY_CTRL_Q:    exit(0);                    break;
+        default:            insert(key);                break;
     }
 }