aboutsummaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-19 13:35:18 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-19 13:35:18 +0200
commita85d110f2859d3e44547b43d1948080c78b6d867 (patch)
tree8f7b14e6274b4ca4afea5481a58221815ccaf0a0 /text.c
parentffa12d93c7efc516ead1dfc798f9afc0d3ec1aa1 (diff)
downloadvis-a85d110f2859d3e44547b43d1948080c78b6d867.tar.gz
vis-a85d110f2859d3e44547b43d1948080c78b6d867.tar.xz
Support files with Windows style newlines \r\n
Diffstat (limited to 'text.c')
-rw-r--r--text.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/text.c b/text.c
index 28115fd..2f3ea17 100644
--- a/text.c
+++ b/text.c
@@ -120,6 +120,7 @@ struct Text {
int fd; /* the file descriptor of the original mmap-ed data */
LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */
const char *marks[26]; /* a mark is a pointer into an underlying buffer */
+ int newlines; /* 0: unknown, 1: \n, -1: \r\n */
};
/* buffer management */
@@ -832,6 +833,20 @@ bool text_modified(Text *txt) {
return txt->saved_action != txt->undo;
}
+bool text_newlines_crnl(Text *txt){
+ if (txt->newlines == 0) {
+ txt->newlines = 1; /* default to UNIX style \n new lines */
+ const char *start = txt->buf.data;
+ if (start) {
+ const char *nl = memchr(start, '\n', txt->buf.len);
+ if (nl > start && nl[-1] == '\r')
+ txt->newlines = -1; /* Windows style \r\n */
+ }
+ }
+
+ return txt->newlines < 0;
+}
+
static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) {
*it = (Iterator){
.pos = pos,