From: Michael D. Lowis Date: Sun, 21 May 2017 01:14:50 +0000 (-0400) Subject: Added logic to trim trailing whitespace on save X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0bd63c241bf366427968a592722d386988faa78b;p=projs%2Ftide.git Added logic to trim trailing whitespace on save --- diff --git a/TODO.md b/TODO.md index 563aadd..0709aac 100644 --- a/TODO.md +++ b/TODO.md @@ -2,14 +2,14 @@ Up Next: -* refactor selection handling to buf.c to prepare fo rmultiple selections. -* refactor x11.c and win.c -* Make Fn keys execute nth command in the tags buffers * Run commands in the background and don't block the main thread. +* refactor selection handling to buf.c to prepare for multiple selections. +* right click to fetch file or line +* Make Fn keys execute nth command in the tags buffers * check for file changes on save * check for file changes when window regains focus * 100% coverage with unit and unit-integration tests -* right click to fetch file or line +* refactor x11.c and win.c * Status line should omit characters from beginning of path to make file path fit Straight-up Bugs: diff --git a/inc/edit.h b/inc/edit.h index fd5a2c5..5670ecf 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -221,6 +221,7 @@ enum { BufSize = 8192, /* default buffer size */ FontCacheSize = 16, /* Maximum number of fonts allowed in the font cache */ EventTimeout = 100, /* Maximum blocking wait time for events */ + TrimOnSave = 1, /* Enable trimming of trailing whitespace on save */ #ifdef __MACH__ LineSpacing = 0, /* Number of pixels for spacing between lines */ #else diff --git a/xedit.c b/xedit.c index e8c6a55..6f9b796 100644 --- a/xedit.c +++ b/xedit.c @@ -127,7 +127,24 @@ static void quit(void) { } static void save(void) { - buf_save(win_buf(EDIT)); + Buf* buf = win_buf(EDIT); + if (TrimOnSave) { + View* view = win_view(EDIT); + unsigned off = 0; + while (buf_end(buf) && (off < buf_end(buf)-1)) { + off = buf_eol(buf, off); + Rune r = buf_get(buf, off-1); + for (; (r == ' ' || r == '\t'); r = buf_get(buf, off-1)) { + if (off <= view->selection.beg) { + view->selection.end--; + view->selection.beg--; + } + off = buf_delete(buf, off-1, off); + } + off = buf_byline(buf, off, +1); + } + } + buf_save(buf); } /* Mouse Handling