Skip to content

Commit

Permalink
view: try to improve cursor placement
Browse files Browse the repository at this point in the history
This changes which viewport is being displayed after the primary
cursor moves out of the currently viewable area.

Close #164, #274, #278
  • Loading branch information
martanne committed Apr 29, 2016
1 parent 9b68bc5 commit c8d1e22
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,8 @@ void view_redraw_bottom(View *view) {
return;
size_t pos = view->cursor->pos;
view_viewport_up(view, view->height);
while (pos > view->end && view_viewport_down(view, 1));
view_cursor_to(view, pos);
while (pos >= view->end && view_viewport_down(view, 1));
cursor_to(view->cursor, pos);
}

size_t view_slide_up(View *view, int lines) {
Expand Down Expand Up @@ -1310,25 +1310,28 @@ void view_cursors_scroll_to(Cursor *c, size_t pos) {
void view_cursors_to(Cursor *c, size_t pos) {
View *view = c->view;
if (c->view->cursor == c) {
c->mark = text_mark_set(view->text, pos);
/* make sure we redraw changes to the very first character of the window */
if (view->start == pos)
view->start_last = 0;

if (view->end == pos && view->lastline == view->bottomline) {
view->start += view->topline->len;
view_draw(view);
}

size_t max = text_size(view->text);
if (pos == max && view->end < max) {
/* do not display an empty screen when shoviewg the end of the file */
if (pos < view->start || pos > view->end) {
view->start = pos;
view_viewport_up(view, view->height / 2);

This comment has been minimized.

Copy link
@erf

erf Apr 29, 2016

Contributor

If you remove / 2, the view will stay at the bottom of the selection / input, and works better IMO
.. but this causes the view to move one screen up when moving the cursor above top of screen.

} else {
/* make sure we redraw changes to the very first character of the window */
if (view->start == pos)
view->start_last = 0;
/* set the start of the viewable region to the start of the line on which
* the cursor should be placed. if this line requires more space than
* available in the view then simply start displaying text at the new
* cursor position */
for (int i = 0; i < 2 && (pos <= view->start || pos > view->end); i++) {
view->start = i == 0 ? text_line_begin(view->text, pos) : pos;
view_draw(view);
}
}

if (pos <= view->start || pos > view->end) {
view->start = text_line_begin(view->text, pos);
view_draw(view);
}

if (pos <= view->start || pos > view->end) {
view->start = pos;
view_draw(view);
}
}

Expand Down

0 comments on commit c8d1e22

Please sign in to comment.