From 4a67c4103d43ff0d65cfba68e7ab6c46d9fe4b4c Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 31 Dec 2016 23:22:21 -0500 Subject: [PATCH] Presence of tabs in the opened file turns of expand_tabs. This should do the right thing in most cases. for everything else theres the Tabs tagcommand --- TODO.md | 1 - inc/edit.h | 2 +- libedit/buf.c | 5 ++--- libedit/charset.c | 32 +++++++++++++++++--------------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/TODO.md b/TODO.md index f277b74..06b3278 100644 --- a/TODO.md +++ b/TODO.md @@ -18,7 +18,6 @@ The Rest: * Add a SaveAs tag that takes an argument for the filename to save as * Add a GoTo tag for ctags lookup and line number jump * Use select to check for error strings in exec.c -* Expand tabs setting should be disabled if opened file contains tabs * Add a tools dir to namespace utility scripts only useful inside the editor * shift+click to extend selection * implement command diffing logic to optimize the undo/redo log diff --git a/inc/edit.h b/inc/edit.h index 60553bb..6fd6517 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -102,7 +102,7 @@ enum { UTF_32LE, /* UTF-32 encoding, little-endian */ }; -int charset(const uint8_t* buf, size_t len, int* crlf); +void filetype(Buf* buf, FMap file); void utf8load(Buf* buf, FMap file); void utf8save(Buf* buf, FILE* file); void binload(Buf* buf, FMap file); diff --git a/libedit/buf.c b/libedit/buf.c index 56fb288..e36d375 100644 --- a/libedit/buf.c +++ b/libedit/buf.c @@ -200,7 +200,7 @@ unsigned buf_load(Buf* buf, char* path) { /* load the file and determine the character set */ FMap file = mmap_readonly(buf->path); - buf->charset = (file.buf ? charset(file.buf, file.len, &buf->crlf) : UTF_8); + filetype(buf, file); if (buf->charset > UTF_8) die("Unsupported character set"); @@ -218,8 +218,7 @@ unsigned buf_load(Buf* buf, char* path) { /* reset buffer state */ buf->modified = false; - free(buf->undo); - buf->undo = NULL; + buf_logclear(buf); return off; } diff --git a/libedit/charset.c b/libedit/charset.c index dad2208..173d120 100644 --- a/libedit/charset.c +++ b/libedit/charset.c @@ -25,24 +25,26 @@ 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* crlf) { - size_t crs = 0; - size_t lfs = 0; +void filetype(Buf* buf, FMap file) { + size_t crs = 0, lfs = 0, tabs = 0; /* look for a BOM and parse it */ for (size_t i = 0; i < (sizeof(BOMS)/sizeof(BOMS[0])); i++) if (!strncmp((char*)buf, (char*)BOMS[i].seq, BOMS[i].len)) - return BOMS[i].type; - /* look for bytes that are invalid in utf-8 */ - int type = UTF_8; - size_t i = 0; - for (i = 0; i < len; i++) { - type &= Utf8Valid[(int)buf[i]]; - switch(buf[i]) { - case '\r': crs++; break; - case '\n': lfs++; break; + buf->charset = BOMS[i].type; + /* look for bytes that are invalid in utf-8 and count tabs, carriage + returns, and line feeds */ + int type = buf->charset; + for (size_t i = 0; i < file.len; i++) { + if (type == UTF_8) + type &= Utf8Valid[(int)file.buf[i]]; + switch(file.buf[i]) { + case '\r': crs++; break; + case '\n': lfs++; break; + case '\t': tabs++; break; } } - /* report back the linefeed mode */ - *crlf = (crs == lfs); - return type; + /* setup filetype attributes in the buffer */ + buf->crlf = (crs == lfs); + buf->charset = type; + buf->expand_tabs = (tabs == 0); } -- 2.51.0