summaryrefslogtreecommitdiff
path: root/src/dged/buffer.c
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2025-11-05 23:17:10 +0100
committerAlbert Cervin <albert@acervin.com>2025-11-05 23:17:10 +0100
commitd131f7964b04ad71b2bda397beee2aba63a43332 (patch)
tree22003b212e33333d5ef05557eb159166a97c0e3c /src/dged/buffer.c
parent7f0bd82cfaff98072bd49db6e6308579d013f523 (diff)
downloaddged-d131f7964b04ad71b2bda397beee2aba63a43332.tar.gz
dged-d131f7964b04ad71b2bda397beee2aba63a43332.tar.xz
dged-d131f7964b04ad71b2bda397beee2aba63a43332.zip
Support CRLF line endings
It now detects and saves properly.
Diffstat (limited to 'src/dged/buffer.c')
-rw-r--r--src/dged/buffer.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c
index e428c58..d03f3ad 100644
--- a/src/dged/buffer.c
+++ b/src/dged/buffer.c
@@ -1,5 +1,6 @@
#include "buffer.h"
#include "binding.h"
+#include "dged/text.h"
#include "dged/vec.h"
#include "display.h"
#include "errno.h"
@@ -237,7 +238,7 @@ static void buffer_read_from_file(struct buffer *b) {
undo_push_boundary(&b->undo, (struct undo_boundary){.save_point = true});
}
-static void write_line(struct text_chunk *chunk, void *userdata) {
+static void write_line_lf(struct text_chunk *chunk, void *userdata) {
FILE *file = (FILE *)userdata;
fwrite(chunk->text, 1, chunk->nbytes, file);
@@ -245,6 +246,15 @@ static void write_line(struct text_chunk *chunk, void *userdata) {
fputc('\n', file);
}
+static void write_line_crlf(struct text_chunk *chunk, void *userdata) {
+ FILE *file = (FILE *)userdata;
+ fwrite(chunk->text, 1, chunk->nbytes, file);
+
+ // final newline is not optional!
+ fputc('\r', file);
+ fputc('\n', file);
+}
+
static bool is_word_break(const struct codepoint *codepoint) {
uint32_t c = codepoint->codepoint;
return c == ' ' || c == '.' || c == '(' || c == ')' || c == '[' || c == ']' ||
@@ -417,7 +427,14 @@ void buffer_to_file(struct buffer *buffer) {
if (nlines > 0) {
struct text_chunk lastline = text_get_line(buffer->text, nlines - 1);
nlines_to_write = lastline.nbytes == 0 ? nlines - 1 : nlines;
- text_for_each_line(buffer->text, 0, nlines_to_write, write_line, file);
+ switch (text_get_line_ending(buffer->text)) {
+ case LineEnding_CRLF:
+ text_for_each_line(buffer->text, 0, nlines_to_write, write_line_crlf,
+ file);
+ break;
+ default:
+ text_for_each_line(buffer->text, 0, nlines_to_write, write_line_lf, file);
+ }
}
minibuffer_echo_timeout(4, "wrote %d lines to %s", nlines_to_write,