From 4f15fee811d2645bf810a475d4272f83c30608d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 8 Apr 2017 22:11:57 +0200 Subject: [PATCH] vis: remove handling of \r\n line endings Use something like dos2unix(1) and unix2dos(1), if you need to edit such files. --- lua/vis-std.lua | 4 ---- test | 2 +- text.c | 32 +------------------------------- text.h | 9 --------- view.c | 9 --------- vis-lua.c | 22 ---------------------- vis-operators.c | 4 ++-- vis.c | 13 ++++++------- vis.h | 3 +-- 9 files changed, 11 insertions(+), 87 deletions(-) diff --git a/lua/vis-std.lua b/lua/vis-std.lua index 01d137a..e762d86 100644 --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -91,10 +91,6 @@ vis.events.subscribe(vis.events.WIN_STATUS, function(win) table.insert(left_parts, (file.name or '[No Name]') .. (file.modified and ' [+]' or '') .. (vis.recording and ' @' or '')) - if file.newlines == "crlf" then - table.insert(right_parts, "␍␊") - end - if #win.cursors > 1 then table.insert(right_parts, cursor.number..'/'..#win.cursors) end diff --git a/test b/test index e0e6400..462ac8a 160000 --- a/test +++ b/test @@ -1 +1 @@ -Subproject commit e0e6400c61a4d74e68a2d2ef0039d077b8955853 +Subproject commit 462ac8a78d8b5c21ecb4ec00dff2316d9f85d6ea diff --git a/text.c b/text.c index c4d354e..7b197ca 100644 --- a/text.c +++ b/text.c @@ -124,7 +124,6 @@ struct Text { size_t size; /* current file content size in bytes */ struct stat info; /* stat as probed at load time */ LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */ - enum TextNewLine newlines; /* which type of new lines does the file use */ }; struct TextSave { /* used to hold context between text_save_{begin,commit} calls */ @@ -669,9 +668,7 @@ bool text_printf(Text *txt, size_t pos, const char *format, ...) { } size_t text_insert_newline(Text *txt, size_t pos) { - const char *data = text_newline_char(txt); - size_t len = strlen(data); - return text_insert(txt, pos, data, len) ? len : 0; + return text_insert(txt, pos, "\n", 1) ? 1 : 0; } static size_t revision_undo(Text *txt, Revision *rev) { @@ -1321,33 +1318,6 @@ bool text_sigbus(Text *txt, const char *addr) { return false; } -enum TextNewLine text_newline_type(Text *txt){ - if (!txt->newlines) { - txt->newlines = TEXT_NEWLINE_LF; /* default to UNIX style \n new lines */ - const char *start = txt->block ? txt->block->data : NULL; - if (start) { - const char *nl = memchr(start, '\n', txt->block->len); - if (nl > start && nl[-1] == '\r') - txt->newlines = TEXT_NEWLINE_CRLF; - } 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_CRLF; - } - } - - return txt->newlines; -} - -const char *text_newline_char(Text *txt) { - static const char *types[] = { - [TEXT_NEWLINE_LF] = "\n", - [TEXT_NEWLINE_CRLF] = "\r\n", - }; - return types[text_newline_type(txt)]; -} - static bool text_iterator_init(Iterator *it, size_t pos, Piece *p, size_t off) { Iterator iter = (Iterator){ .pos = pos, diff --git a/text.h b/text.h index da97a5d..deb39c3 100644 --- a/text.h +++ b/text.h @@ -121,15 +121,6 @@ bool text_modified(Text*); * this text instance */ bool text_sigbus(Text*, const char *addr); -/* which type of new lines does the text use? */ -enum TextNewLine { - TEXT_NEWLINE_LF = 1, - TEXT_NEWLINE_CRLF, -}; - -enum TextNewLine text_newline_type(Text*); -const char *text_newline_char(Text*); - enum TextSaveMethod { TEXT_SAVE_AUTO, /* first try atomic, then fall back to inplace */ TEXT_SAVE_ATOMIC, /* create a new file, write content, atomically rename(2) over old file */ diff --git a/view.c b/view.c index 49b0d4f..7e893ff 100644 --- a/view.c +++ b/view.c @@ -382,11 +382,6 @@ void view_draw(View *view) { cell.width = 1; } - if (cur[0] == '\r' && rem > 1 && cur[1] == '\n') { - /* convert views style newline \r\n into a single char with len = 2 */ - cell = (Cell){ .data = "\n", .len = 2, .width = 1 }; - } - if (cell.width == 0 && prev_cell.len + cell.len < sizeof(cell.data)) { prev_cell.len += cell.len; strcat(prev_cell.data, cell.data); @@ -593,16 +588,12 @@ bool view_viewport_up(View *view, int n) { /* skip newlines immediately before display area */ if (c == '\n' && text_iterator_byte_prev(&it, &c)) off++; - if (c == '\r' && text_iterator_byte_prev(&it, &c)) - off++; do { if (c == '\n' && --n == 0) break; if (++off > max) break; } while (text_iterator_byte_prev(&it, &c)); - if (c == '\r') - off++; view->start -= MIN(view->start, off); view_draw(view); return true; diff --git a/vis-lua.c b/vis-lua.c index e9c5fcf..80e3e38 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -79,9 +79,6 @@ static void window_status_update(Vis *vis, Win *win) { vis_macro_recording(vis) ? " @": ""); left_count++; - if (text_newline_type(txt) == TEXT_NEWLINE_CRLF) - strcpy(right_parts[right_count++], "␍␊"); - int cursor_count = view_cursors_count(view); if (cursor_count > 1) { Cursor *c = view_cursors_primary_get(view); @@ -1872,10 +1869,6 @@ static const struct luaL_Reg window_cursor_funcs[] = { * end */ /*** - * Type of line endings. - * @tfield string newlines the type of line endings either `lf` (the default) or `crlf` - */ -/*** * File size in bytes. * @tfield int size the current file size in bytes */ @@ -1907,21 +1900,6 @@ static int file_index(lua_State *L) { return 1; } - if (strcmp(key, "newlines") == 0) { - switch (text_newline_type(file->text)) { - case TEXT_NEWLINE_LF: - lua_pushstring(L, "lf"); - break; - case TEXT_NEWLINE_CRLF: - lua_pushstring(L, "crlf"); - break; - default: - lua_pushnil(L); - break; - } - return 1; - } - if (strcmp(key, "size") == 0) { lua_pushunsigned(L, text_size(file->text)); return 1; diff --git a/vis-operators.c b/vis-operators.c index 446260d..1ef32c3 100644 --- a/vis-operators.c +++ b/vis-operators.c @@ -48,7 +48,7 @@ static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) { case VIS_OP_PUT_AFTER_END: if (c->reg->linewise && !sel_linewise) pos = text_line_next(txt, pos); - else if (!sel && text_byte_get(txt, pos, &b) && b != '\r' && b != '\n') + else if (!sel && text_byte_get(txt, pos, &b) && b != '\n') pos = text_char_next(txt, pos); break; case VIS_OP_PUT_BEFORE: @@ -218,7 +218,7 @@ static size_t op_join(Vis *vis, Text *txt, OperatorContext *c) { text_delete(txt, pos, end - pos); char prev, next; if (text_byte_get(txt, pos-1, &prev) && !isspace((unsigned char)prev) && - text_byte_get(txt, pos, &next) && next != '\r' && next != '\n') + text_byte_get(txt, pos, &next) && next != '\n') text_insert(txt, pos, c->arg->s, len); if (mark == EMARK) mark = text_mark_set(txt, pos); diff --git a/vis.c b/vis.c index e63d206..82b47b9 100644 --- a/vis.c +++ b/vis.c @@ -772,7 +772,7 @@ void vis_replace(Vis *vis, size_t pos, const char *data, size_t len) { Text *txt = vis->win->file->text; Iterator it = text_iterator_get(txt, pos); int chars = text_char_count(data, len); - for (char c; chars-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n'; ) + for (char c; chars-- > 0 && text_iterator_byte_get(&it, &c) && c != '\n'; ) text_iterator_char_next(&it, NULL); text_delete(txt, pos, it.pos - pos); @@ -1601,8 +1601,7 @@ void vis_insert_tab(Vis *vis) { } size_t vis_text_insert_nl(Vis *vis, Text *txt, size_t pos) { - const char *nl = text_newline_char(txt); - size_t nl_len = strlen(nl), indent_len = 0; + size_t indent_len = 0; char byte, *indent = NULL; /* insert second newline at end of file, except if there is already one */ bool eof = pos == text_size(txt); @@ -1625,14 +1624,14 @@ size_t vis_text_insert_nl(Vis *vis, Text *txt, size_t pos) { } } - text_insert(txt, pos, nl, nl_len); + text_insert(txt, pos, "\n", 1); if (eof) { if (nl2) - text_insert(txt, pos, nl, nl_len); + text_insert(txt, pos, "\n", 1); else - pos -= nl_len; /* place cursor before, not after nl */ + pos--; /* place cursor before, not after nl */ } - pos += nl_len; + pos++; if (indent) text_insert(txt, pos, indent, indent_len); diff --git a/vis.h b/vis.h index 9b9fbb9..5b49a15 100644 --- a/vis.h +++ b/vis.h @@ -156,8 +156,7 @@ void vis_replace_key(Vis*, const char *data, size_t len); /* inserts a tab (peforms tab expansion based on current editing settings), * at all current cursor positions */ void vis_insert_tab(Vis*); -/* inserts a new line sequence (depending on the file type this might be \n or - * \r\n) at all current cursor positions */ +/* inserts a \n character at every cursor position */ void vis_insert_nl(Vis*); /* processes the given command line arguments and starts the main loop, won't -- 2.11.4.GIT