diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-04-22 21:20:59 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-04-22 21:34:40 +0200 |
| commit | 287521cba09765d9d0901b79d8911a27de1af4e7 (patch) | |
| tree | 8a505debc63763bca159ba0b6c60cae02d583d9e | |
| parent | 6d7794c0356902bc816923f4409d9d39a62db171 (diff) | |
| download | vis-287521cba09765d9d0901b79d8911a27de1af4e7.tar.gz vis-287521cba09765d9d0901b79d8911a27de1af4e7.tar.xz | |
Cleanup line ending type detection and insertion
| -rw-r--r-- | text.c | 20 | ||||
| -rw-r--r-- | text.h | 11 | ||||
| -rw-r--r-- | vis.c | 13 |
3 files changed, 33 insertions, 11 deletions
@@ -118,7 +118,7 @@ struct Text { struct stat info; /* stat as proped on load time */ int fd; /* the file descriptor of the original mmap-ed data */ LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */ - int newlines; /* 0: unknown, 1: \n, -1: \r\n */ + enum TextNewLine newlines; /* which type of new lines does the file use */ }; /* buffer management */ @@ -902,18 +902,23 @@ 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 */ +enum TextNewLine text_newline_type(Text *txt){ + if (!txt->newlines) { + txt->newlines = TEXT_NEWLINE_NL; /* 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 */ + txt->newlines = TEXT_NEWLINE_CRNL; + } else { + char c; + size_t nl = lines_skip_forward(txt, 0, 1, NULL); + if (nl > 1 && text_byte_get(txt, nl-2, &c) && c == '\r') + txt->newlines = TEXT_NEWLINE_CRNL; } } - return txt->newlines < 0; + return txt->newlines; } static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) { @@ -1088,7 +1093,8 @@ static size_t lines_skip_forward(Text *txt, size_t pos, size_t lines, size_t *li if (lines == 0) break; } - *lines_skipped = lines_old - lines; + if (lines_skipped) + *lines_skipped = lines_old - lines; return pos; } @@ -82,8 +82,15 @@ size_t text_history_get(Text*, size_t index); size_t text_size(Text*); bool text_modified(Text*); -/* test whether the underlying file uses UNIX style \n or Windows style \r\n newlines */ -bool text_newlines_crnl(Text*); + +/* which type of new lines does the text use? */ +enum TextNewLine { + TEXT_NEWLINE_NL = 1, + TEXT_NEWLINE_CRNL, +}; + +enum TextNewLine text_newline_type(Text*); + bool text_save(Text*, const char *file); bool text_range_save(Text*, Filerange*, const char *file); ssize_t text_write(Text*, int fd); @@ -1092,8 +1092,17 @@ static void copy_indent_from_previous_line(View *view, Text *text) { } static void insert_newline(const Arg *arg) { - insert(&(const Arg){ .s = - text_newlines_crnl(vis->win->file->text) ? "\r\n" : "\n" }); + const char *nl; + switch (text_newline_type(vis->win->file->text)) { + case TEXT_NEWLINE_CRNL: + nl = "\r\n"; + break; + default: + nl = "\n"; + break; + } + + insert(&(const Arg){ .s = nl }); if (vis->autoindent) copy_indent_from_previous_line(vis->win->view, vis->win->file->text); |
