]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added logic to trim trailing whitespace on save
authorMichael D. Lowis <mike@mdlowis.com>
Sun, 21 May 2017 01:14:50 +0000 (21:14 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sun, 21 May 2017 01:14:50 +0000 (21:14 -0400)
TODO.md
inc/edit.h
xedit.c

diff --git a/TODO.md b/TODO.md
index 563aadd1fd33c28b7348017969968a2302d78787..0709aac2cf085aa83a2efd8dfa24ba51b6071140 100644 (file)
--- 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:
index fd5a2c59d47310a938b239af1eca7f71eedd9f1c..5670ecfee671aee45beee565e609cfddc0d0ef4c 100644 (file)
@@ -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 e8c6a55d678b17b155fe8061d90279da104e1df2..6f9b79633505de1e6e5f5c410475ba7e0caf7fd3 100644 (file)
--- 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