diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2017-01-20 09:11:41 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2017-01-20 15:05:38 +0100 |
| commit | 181fe9aae12c6f25d8e5a10309c4e551bef338dd (patch) | |
| tree | 3109751b761e5869e4824ebfdb2322fa85f4e6c1 /vis.c | |
| parent | 21dcbfe451be24786ac8f22e022cda07d17413d5 (diff) | |
| download | vis-181fe9aae12c6f25d8e5a10309c4e551bef338dd.tar.gz vis-181fe9aae12c6f25d8e5a10309c4e551bef338dd.tar.xz | |
vis: improve new line handling at end of file
<Enter> at the end of the file now inserts two newlines, unless there is
already one in place. This ensures that in 'normal' operation the file
is always new line terminated (as mandated by POSIX). It also means that
there is no problem displaying the right amount of ~ symbols at the end
of the file.
Unlike in vim the cell beyond the end of the file remains adressable
even in normal mode. This means something like the following (starting
from an empty file) might be a little confusing:
o<Escape><Left>dd
Because the starting position is beyond the last newline of the file,
nothing will be deleted.
For now we prefer to avoid the additional complexity, and difference
in behavior between normal and insert mode, needed to fix this slight
inconsistency.
Fix #294
Diffstat (limited to 'vis.c')
| -rw-r--r-- | vis.c | 20 |
1 files changed, 18 insertions, 2 deletions
@@ -1421,8 +1421,24 @@ static void copy_indent_from_previous_line(Win *win, Cursor *cur) { } void vis_insert_nl(Vis *vis) { - const char *nl = text_newline_char(vis->win->file->text); - vis_insert_key(vis, nl, strlen(nl)); + Text *txt = vis->win->file->text; + const char *nl = text_newline_char(txt); + size_t len = strlen(nl); + for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) { + char byte; + size_t pos = view_cursors_pos(c); + /* insert second newline at end of file, except if there is already one */ + bool eof = pos == text_size(txt); + bool nl2 = eof && !(pos > 0 && text_byte_get(txt, pos-1, &byte) && byte == '\n'); + vis_insert(vis, pos, nl, len); + if (eof) { + if (nl2) + vis_insert(vis, pos, nl, len); + else + pos -= len; /* place cursor before, not after nl */ + } + view_cursors_scroll_to(c, pos + len); + } if (!vis->autoindent) return; |
