From: Michael D. Lowis Date: Sun, 16 Oct 2016 02:51:17 +0000 (-0400) Subject: enter key now inserts \r\n in crlf mode X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4856e68267830bea048f5380d85caee500d1d297;p=projs%2Ftide.git enter key now inserts \r\n in crlf mode --- diff --git a/buf.c b/buf.c index c7e8f49..8e1d217 100644 --- 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"); diff --git a/charset.c b/charset.c index e002f1f..0900bc3 100644 --- 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 fe48303..2d34cc1 100644 --- 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 diff --git a/keyboard.c b/keyboard.c index 0a970fb..d6d2b09 100644 --- a/keyboard.c +++ b/keyboard.c @@ -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; } }