diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-06-30 11:13:17 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-06-30 11:13:17 +0200 |
| commit | 60fe132d6c59d54021091af8e5aa09fcd4704562 (patch) | |
| tree | 506249a4ebd7a09c0e2433c2a920c8c89a1ca4a8 /view.c | |
| parent | 82a10e051c593e133554ce9675169fd68bb02ad7 (diff) | |
| download | vis-60fe132d6c59d54021091af8e5aa09fcd4704562.tar.gz vis-60fe132d6c59d54021091af8e5aa09fcd4704562.tar.xz | |
Cleanup insert/replace mode input handling
View should only display the file content, but not modify it.
Diffstat (limited to 'view.c')
| -rw-r--r-- | view.c | 80 |
1 files changed, 6 insertions, 74 deletions
@@ -59,16 +59,6 @@ static bool view_addch(View *view, Cell *cell); static size_t view_cursor_update(View *view); /* set/move current cursor position to a given (line, column) pair */ static size_t view_cursor_set(View *view, Line *line, int col); -/* move visible viewport n-lines up/down, redraws the view but does not change - * cursor position which becomes invalid and should be corrected by either: - * - * - view_cursor_to - * - view_cursor_set - * - * the return value indicates wether the visible area changed. - */ -static bool view_viewport_up(View *view, int n); -static bool view_viewport_down(View *view, int n); void view_tabwidth_set(View *view, int tabwidth) { view->tabwidth = tabwidth; @@ -237,6 +227,10 @@ CursorPos view_cursor_getpos(View *view) { return pos; } +ViewPos view_cursor_viewpos(View *view) { + return (ViewPos){ .x = view->cursor.col, .y = view->cursor.row }; +} + /* snyc current cursor position with internal Line/Cell structures */ static void view_cursor_sync(View *view) { int row = 0, col = 0; @@ -596,7 +590,7 @@ static size_t view_cursor_set(View *view, Line *line, int col) { return pos; } -static bool view_viewport_down(View *view, int n) { +bool view_viewport_down(View *view, int n) { Line *line; if (view->end == text_size(view->text)) return false; @@ -610,7 +604,7 @@ static bool view_viewport_down(View *view, int n) { return true; } -static bool view_viewport_up(View *view, int n) { +bool view_viewport_up(View *view, int n) { /* scrolling up is somewhat tricky because we do not yet know where * the lines start, therefore scan backwards but stop at a reasonable * maximum in case we are dealing with a file without any newlines @@ -797,68 +791,6 @@ size_t view_screenline_end(View *view) { return view_cursor_set(view, cursor->line, col >= 0 ? col : 0); } -size_t view_delete_key(View *view) { - Cursor *cursor = &view->cursor; - Line *line = cursor->line; - size_t len = line->cells[cursor->col].len; - text_delete(view->text, cursor->pos, len); - view_draw(view); - view_cursor_to(view, cursor->pos); - return cursor->pos; -} - -size_t view_backspace_key(View *view) { - Cursor *cursor = &view->cursor; - if (view->start == cursor->pos) { - if (view->start == 0) - return cursor->pos; - /* if we are on the top left most position in the view - * first scroll up so that the to be deleted character is - * visible then proceed as normal */ - size_t pos = cursor->pos; - view_viewport_up(view, 1); - view_cursor_to(view, pos); - } - view_char_prev(view); - size_t pos = cursor->pos; - size_t len = cursor->line->cells[cursor->col].len; - text_delete(view->text, pos, len); - view_draw(view); - view_cursor_to(view, pos); - return pos; -} - -size_t view_insert_key(View *view, const char *c, size_t len) { - size_t pos = view->cursor.pos; - text_insert(view->text, pos, c, len); - if (view->cursor.line == view->bottomline && memchr(c, '\n', len)) - view_viewport_down(view, 1); - else - view_draw(view); - pos += len; - view_cursor_to(view, pos); - return pos; -} - -size_t view_replace_key(View *view, const char *c, size_t len) { - Cursor *cursor = &view->cursor; - Line *line = cursor->line; - size_t pos = cursor->pos; - /* do not overwrite new line which would merge the two lines */ - if (line->cells[cursor->col].data[0] != '\n') { - size_t oldlen = line->cells[cursor->col].len; - text_delete(view->text, pos, oldlen); - } - text_insert(view->text, pos, c, len); - if (cursor->line == view->bottomline && memchr(c, '\n', len)) - view_viewport_down(view, 1); - else - view_draw(view); - pos += len; - view_cursor_to(view, pos); - return pos; -} - size_t view_cursor_get(View *view) { return view->cursor.pos; } |
