]> git.mdlowis.com Git - projs/tide.git/commitdiff
Make sure files end in newlines
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 17 Mar 2017 17:18:41 +0000 (13:18 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 17 Mar 2017 17:18:41 +0000 (13:18 -0400)
TODO.md
docs/crlf.txt
docs/lf.txt
lib/buf.c

diff --git a/TODO.md b/TODO.md
index c43e099caca802a0eed492dfe70c0f02c7cc7f8d..2efdf12ae3dbd763c68002bc21b5a0c2b381b058 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -3,7 +3,6 @@
 Up Next:
 
 * picker should only draw visible lines
-* files should always end in newlines
 * ctrl+d on last line segfaults/asserts
 
 * Tweak the scrollbar for use with the picker
index e6c3387d26c4e9e5ac97ccfef1cbb5883df76250..dab8c54834f67ab46a81f3dab41b2ad25159d94d 100644 (file)
Binary files a/docs/crlf.txt and b/docs/crlf.txt differ
index e44b242ac08300453d82417530a078ea4225821c..d8027d5e9219eeac408fb77c753dd6d644fe810c 100644 (file)
Binary files a/docs/lf.txt and b/docs/lf.txt differ
index 22e12812247209f4c1e22f4fec65282a891cf706..7164974a9e5b2cabbac9b09a7ba7eb0e60f36b4a 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -234,6 +234,12 @@ unsigned buf_load(Buf* buf, char* path) {
 
 void buf_save(Buf* buf) {
     if (0 == buf_end(buf)) return;
+    
+    /* text files should  always end in a new line. If we detected a 
+       binary file or at least a non-utf8 file, skip this part. */
+    if (!buf_iseol(buf, buf_end(buf)-1) && (buf->charset != BINARY))
+        buf_insert(buf, false, buf_end(buf), '\n');
+    
     size_t wrlen = 0;
     if (!buf->path) return;
     FMap file = mmap_readwrite(buf->path, buf_end(buf) * UTF_MAX);