diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-10-26 20:33:25 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-10-27 11:13:07 +0100 |
| commit | bc9909f4cd9355c197944ee320eed3971431f699 (patch) | |
| tree | 4c15a2ad91cfbe3eeea73969dd9d08dce1987775 /vis.c | |
| parent | 321cf83fba85c70d0bdc89a07e8af4148127f23c (diff) | |
| download | vis-bc9909f4cd9355c197944ee320eed3971431f699.tar.gz vis-bc9909f4cd9355c197944ee320eed3971431f699.tar.xz | |
vis: clean up tab/newline insertion code
Diffstat (limited to 'vis.c')
| -rw-r--r-- | vis.c | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -1083,7 +1083,7 @@ static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) { return pos; } -const char *vis_expandtab(Vis *vis) { +static const char *expandtab(Vis *vis) { static char spaces[9]; int tabwidth = tabwidth_get(vis); tabwidth = MIN(tabwidth, LENGTH(spaces) - 1); @@ -1095,7 +1095,7 @@ const char *vis_expandtab(Vis *vis) { static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) { size_t pos = text_line_begin(txt, c->range.end), prev_pos; - const char *tab = vis_expandtab(vis); + const char *tab = expandtab(vis); size_t tablen = strlen(tab); /* if range ends at the begin of a line, skip line break */ @@ -3035,3 +3035,43 @@ void vis_exit(Vis *vis, int status) { const char *vis_mode_status(Vis *vis) { return vis->mode->status; } + +void vis_insert_tab(Vis *vis) { + const char *tab = expandtab(vis); + vis_insert_key(vis, tab, strlen(tab)); +} + +static void copy_indent_from_previous_line(Win *win) { + View *view = win->view; + Text *text = win->file->text; + size_t pos = view_cursor_get(view); + size_t prev_line = text_line_prev(text, pos); + if (pos == prev_line) + return; + size_t begin = text_line_begin(text, prev_line); + size_t start = text_line_start(text, begin); + size_t len = start-begin; + char *buf = malloc(len); + if (!buf) + return; + len = text_bytes_get(text, begin, len, buf); + vis_insert_key(win->editor, buf, len); + free(buf); +} + +void vis_insert_nl(Vis *vis) { + const char *nl; + switch (text_newline_type(vis->win->file->text)) { + case TEXT_NEWLINE_CRNL: + nl = "\r\n"; + break; + default: + nl = "\n"; + break; + } + + vis_insert_key(vis, nl, strlen(nl)); + + if (vis->autoindent) + copy_indent_from_previous_line(vis->win); +} |
