Skip to content

Commit 522eb68

Browse files
committed
Add support for trim_trailing_whitespace via EditorConfig
1 parent f560618 commit 522eb68

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Buffer.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ struct Buffer_ {
8585
int saveTabulationY;
8686
// document uses DOS-style ctrl-M
8787
bool dosLineBreaks;
88+
// trim trailing whitespace when saving
89+
bool trimTrailingWhitespace;
8890
// document uses UTF-8 (if false, assume ISO-8859-15)
8991
bool isUTF8;
9092
// time tracker to disable auto-indent when pasting;
@@ -228,6 +230,12 @@ static void Buffer_checkEditorConfig(Buffer* this, const char* fileName) {
228230
} else {
229231
this->isUTF8 = true;
230232
}
233+
} else if (strcmp(name, "trim_trailing_whitespace") == 0) {
234+
if (strcmp(value, "true") == 0) {
235+
this->trimTrailingWhitespace = true;
236+
} else {
237+
this->trimTrailingWhitespace = false;
238+
}
231239
}
232240
}
233241
}
@@ -253,6 +261,7 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command, Tab
253261
this->modified = false;
254262
this->tabulation = 3;
255263
this->dosLineBreaks = false;
264+
this->trimTrailingWhitespace = false;
256265
this->tabSize = 8;
257266
this->nCursors = 0;
258267

@@ -1286,9 +1295,14 @@ Coords Buffer_find(Buffer* this, Text needle, bool findNext, bool caseSensitive,
12861295
return notFound;
12871296
}
12881297

1289-
static void writeLineInFormat(FILE* fd, Line* l, bool utf8, iconv_t cd) {
1298+
static void writeLineInFormat(FILE* fd, Line* l, bool utf8, bool trimTrailingWhitespace, iconv_t cd) {
12901299
char* intext = Line_toString(l);
12911300
size_t insize = Line_bytes(l);
1301+
if (trimTrailingWhitespace) {
1302+
while(insize > 0 && intext[insize - 1] == ' ') {
1303+
insize--;
1304+
}
1305+
}
12921306
if (utf8) {
12931307
fwrite(intext, insize, 1, fd);
12941308
} else {
@@ -1315,7 +1329,7 @@ void Buffer_saveAndCloseFd(Buffer* this, FILE* fd) {
13151329
cd = iconv_open("ISO-8859-15", "UTF-8");
13161330
}
13171331
while (l) {
1318-
writeLineInFormat(fd, l, this->isUTF8, cd);
1332+
writeLineInFormat(fd, l, this->isUTF8, this->trimTrailingWhitespace, cd);
13191333
l = (Line*) l->super.next;
13201334
if (l)
13211335
fwrite("\n", 1, 1, fd);

Structures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ struct Buffer_ {
217217
int saveTabulationY;
218218
// document uses DOS-style ctrl-M
219219
bool dosLineBreaks;
220+
// trim trailing whitespace when saving
221+
bool trimTrailingWhitespace;
220222
// document uses UTF-8 (if false, assume ISO-8859-15)
221223
bool isUTF8;
222224
// time tracker to disable auto-indent when pasting;

0 commit comments

Comments
 (0)