diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-04-22 11:47:38 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-04-22 12:35:15 +0200 |
| commit | 7318931acca4e6928a8014dd00dc66f6f061b6c4 (patch) | |
| tree | ea8bc1b754f7cd2b609fc71ee69ff66a8e6a0eab /window.c | |
| parent | 4d7c630ff37fd0609aa58f60d6bd08fe53e57533 (diff) | |
| download | vis-7318931acca4e6928a8014dd00dc66f6f061b6c4.tar.gz vis-7318931acca4e6928a8014dd00dc66f6f061b6c4.tar.xz | |
More renames, no functional changes
Win -> View, window_* -> view_*
Diffstat (limited to 'window.c')
| -rw-r--r-- | window.c | 706 |
1 files changed, 353 insertions, 353 deletions
@@ -35,66 +35,66 @@ typedef struct { /* cursor position */ bool highlighted; /* true e.g. when cursor is on a bracket */ } Cursor; -struct Win { /* window showing part of a file */ +struct View { /* viewable area, showing part of a file */ Text *text; /* underlying text management */ UiWin *ui; ViewEvent *events; - int width, height; /* window text area size */ + int width, height; /* size of display area */ Filepos start, end; /* currently displayed area [start, end] in bytes from the start of the file */ size_t lines_size; /* number of allocated bytes for lines (grows only) */ - Line *lines; /* win->height number of lines representing window content */ - Line *topline; /* top of the window, first line currently shown */ + Line *lines; /* view->height number of lines representing view content */ + Line *topline; /* top of the view, first line currently shown */ Line *lastline; /* last currently used line, always <= bottomline */ - Line *bottomline; /* bottom of screen, might be unused if lastline < bottomline */ + Line *bottomline; /* bottom of view, might be unused if lastline < bottomline */ Filerange sel; /* selected text range in bytes from start of file */ - Cursor cursor; /* current window cursor position */ - Line *line; /* used while drawing window content, line where next char will be drawn */ - int col; /* used while drawing window content, column where next char will be drawn */ - Syntax *syntax; /* syntax highlighting definitions for this window or NULL */ + Cursor cursor; /* current cursor position within view */ + Line *line; /* used while drawing view content, line where next char will be drawn */ + int col; /* used while drawing view content, column where next char will be drawn */ + Syntax *syntax; /* syntax highlighting definitions for this view or NULL */ int tabwidth; /* how many spaces should be used to display a tab character */ }; -static void window_clear(Win *win); -static bool window_addch(Win *win, Cell *cell); -static size_t window_cursor_update(Win *win); +static void view_clear(View *view); +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 window_cursor_set(Win *win, Line *line, int col); -/* move visible viewport n-lines up/down, redraws the window but does not change +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: * - * - window_cursor_to - * - window_cursor_set + * - view_cursor_to + * - view_cursor_set * * the return value indicates wether the visible area changed. */ -static bool window_viewport_up(Win *win, int n); -static bool window_viewport_down(Win *win, int n); +static bool view_viewport_up(View *view, int n); +static bool view_viewport_down(View *view, int n); -void window_tabwidth_set(Win *win, int tabwidth) { - win->tabwidth = tabwidth; - window_draw(win); +void view_tabwidth_set(View *view, int tabwidth) { + view->tabwidth = tabwidth; + view_draw(view); } -void window_selection_clear(Win *win) { - win->sel = text_range_empty(); - window_draw(win); - window_cursor_update(win); +void view_selection_clear(View *view) { + view->sel = text_range_empty(); + view_draw(view); + view_cursor_update(view); } -/* reset internal window data structures (cell matrix, line offsets etc.) */ -static void window_clear(Win *win) { +/* reset internal view data structures (cell matrix, line offsets etc.) */ +static void view_clear(View *view) { /* calculate line number of first line */ // TODO move elsewhere - win->topline = win->lines; - win->topline->lineno = text_lineno_by_pos(win->text, win->start); - win->lastline = win->topline; + view->topline = view->lines; + view->topline->lineno = text_lineno_by_pos(view->text, view->start); + view->lastline = view->topline; /* reset all other lines */ - size_t line_size = sizeof(Line) + win->width*sizeof(Cell); - size_t end = win->height * line_size; + size_t line_size = sizeof(Line) + view->width*sizeof(Cell); + size_t end = view->height * line_size; Line *prev = NULL; for (size_t i = 0; i < end; i += line_size) { - Line *line = (Line*)(((char*)win->lines) + i); + Line *line = (Line*)(((char*)view->lines) + i); line->width = 0; line->len = 0; line->prev = prev; @@ -102,14 +102,14 @@ static void window_clear(Win *win) { prev->next = line; prev = line; } - win->bottomline = prev ? prev : win->topline; - win->bottomline->next = NULL; - win->line = win->topline; - win->col = 0; + view->bottomline = prev ? prev : view->topline; + view->bottomline->next = NULL; + view->line = view->topline; + view->col = 0; } -Filerange window_selection_get(Win *win) { - Filerange sel = win->sel; +Filerange view_selection_get(View *view) { + Filerange sel = view->sel; if (sel.start > sel.end) { size_t tmp = sel.start; sel.start = sel.end; @@ -117,79 +117,79 @@ Filerange window_selection_get(Win *win) { } if (!text_range_valid(&sel)) return text_range_empty(); - sel.end = text_char_next(win->text, sel.end); + sel.end = text_char_next(view->text, sel.end); return sel; } -void window_selection_set(Win *win, Filerange *sel) { - Cursor *cursor = &win->cursor; - win->sel = *sel; - window_draw(win); - if (win->ui) - win->ui->cursor_to(win->ui, cursor->col, cursor->row); +void view_selection_set(View *view, Filerange *sel) { + Cursor *cursor = &view->cursor; + view->sel = *sel; + view_draw(view); + if (view->ui) + view->ui->cursor_to(view->ui, cursor->col, cursor->row); } -Filerange window_viewport_get(Win *win) { - return (Filerange){ .start = win->start, .end = win->end }; +Filerange view_viewport_get(View *view) { + return (Filerange){ .start = view->start, .end = view->end }; } -/* try to add another character to the window, return whether there was space left */ -static bool window_addch(Win *win, Cell *cell) { - if (!win->line) +/* try to add another character to the view, return whether there was space left */ +static bool view_addch(View *view, Cell *cell) { + if (!view->line) return false; int width; static Cell empty; - size_t lineno = win->line->lineno; + size_t lineno = view->line->lineno; switch (cell->data[0]) { case '\t': - width = win->tabwidth - (win->col % win->tabwidth); + width = view->tabwidth - (view->col % view->tabwidth); for (int w = 0; w < width; w++) { - if (win->col + 1 > win->width) { - win->line = win->line->next; - win->col = 0; - if (!win->line) + if (view->col + 1 > view->width) { + view->line = view->line->next; + view->col = 0; + if (!view->line) return false; - win->line->lineno = lineno; + view->line->lineno = lineno; } if (w == 0) { /* first cell of a tab has a length of 1 */ - win->line->cells[win->col].len = cell->len; - win->line->len += cell->len; + view->line->cells[view->col].len = cell->len; + view->line->len += cell->len; } else { /* all remaining ones have a lenght of zero */ - win->line->cells[win->col].len = 0; + view->line->cells[view->col].len = 0; } /* but all are marked as part of a tabstop */ - win->line->cells[win->col].width = 1; - win->line->cells[win->col].data[0] = ' '; - win->line->cells[win->col].data[1] = '\0'; - win->line->cells[win->col].istab = true; - win->line->cells[win->col].attr = cell->attr; - win->line->width++; - win->col++; + view->line->cells[view->col].width = 1; + view->line->cells[view->col].data[0] = ' '; + view->line->cells[view->col].data[1] = '\0'; + view->line->cells[view->col].istab = true; + view->line->cells[view->col].attr = cell->attr; + view->line->width++; + view->col++; } return true; case '\n': cell->width = 1; - if (win->col + cell->width > win->width) { - win->line = win->line->next; - win->col = 0; - if (!win->line) + if (view->col + cell->width > view->width) { + view->line = view->line->next; + view->col = 0; + if (!view->line) return false; - win->line->lineno = lineno; + view->line->lineno = lineno; } - win->line->cells[win->col] = *cell; - win->line->len += cell->len; - win->line->width += cell->width; - for (int i = win->col + 1; i < win->width; i++) - win->line->cells[i] = empty; - - win->line = win->line->next; - if (win->line) - win->line->lineno = lineno + 1; - win->col = 0; + view->line->cells[view->col] = *cell; + view->line->len += cell->len; + view->line->width += cell->width; + for (int i = view->col + 1; i < view->width; i++) + view->line->cells[i] = empty; + + view->line = view->line->next; + if (view->line) + view->line->lineno = lineno + 1; + view->col = 0; return true; default: if ((unsigned char)cell->data[0] < 128 && !isprint((unsigned char)cell->data[0])) { @@ -203,30 +203,30 @@ static bool window_addch(Win *win, Cell *cell) { }; } - if (win->col + cell->width > win->width) { - for (int i = win->col; i < win->width; i++) - win->line->cells[i] = empty; - win->line = win->line->next; - win->col = 0; + if (view->col + cell->width > view->width) { + for (int i = view->col; i < view->width; i++) + view->line->cells[i] = empty; + view->line = view->line->next; + view->col = 0; } - if (win->line) { - win->line->width += cell->width; - win->line->len += cell->len; - win->line->lineno = lineno; - win->line->cells[win->col] = *cell; - win->col++; + if (view->line) { + view->line->width += cell->width; + view->line->len += cell->len; + view->line->lineno = lineno; + view->line->cells[view->col] = *cell; + view->col++; /* set cells of a character which uses multiple columns */ for (int i = 1; i < cell->width; i++) - win->line->cells[win->col++] = empty; + view->line->cells[view->col++] = empty; return true; } return false; } } -CursorPos window_cursor_getpos(Win *win) { - Cursor *cursor = &win->cursor; +CursorPos view_cursor_getpos(View *view) { + Cursor *cursor = &view->cursor; Line *line = cursor->line; CursorPos pos = { .line = line->lineno, .col = cursor->col }; while (line->prev && line->prev->lineno == pos.line) { @@ -238,12 +238,12 @@ CursorPos window_cursor_getpos(Win *win) { } /* snyc current cursor position with internal Line/Cell structures */ -static void window_cursor_sync(Win *win) { +static void view_cursor_sync(View *view) { int row = 0, col = 0; - size_t cur = win->start, pos = win->cursor.pos; - Line *line = win->topline; + size_t cur = view->start, pos = view->cursor.pos; + Line *line = view->topline; - while (line && line != win->lastline && cur < pos) { + while (line && line != view->lastline && cur < pos) { if (cur + line->len > pos) break; cur += line->len; @@ -252,104 +252,104 @@ static void window_cursor_sync(Win *win) { } if (line) { - int max_col = MIN(win->width, line->width); + int max_col = MIN(view->width, line->width); while (cur < pos && col < max_col) { cur += line->cells[col].len; /* skip over columns occupied by the same character */ while (++col < max_col && line->cells[col].len == 0); } } else { - line = win->bottomline; - row = win->height - 1; + line = view->bottomline; + row = view->height - 1; } - win->cursor.line = line; - win->cursor.row = row; - win->cursor.col = col; + view->cursor.line = line; + view->cursor.row = row; + view->cursor.col = col; } -/* place the cursor according to the screen coordinates in win->{row,col} and - * fire user callback. if a selection is active, redraw the window to reflect +/* place the cursor according to the screen coordinates in view->{row,col} and + * fire user callback. if a selection is active, redraw the view to reflect * its changes. */ -static size_t window_cursor_update(Win *win) { - Cursor *cursor = &win->cursor; - if (win->sel.start != EPOS) { - win->sel.end = cursor->pos; - window_draw(win); - } else if (win->ui && win->syntax) { +static size_t view_cursor_update(View *view) { + Cursor *cursor = &view->cursor; + if (view->sel.start != EPOS) { + view->sel.end = cursor->pos; + view_draw(view); + } else if (view->ui && view->syntax) { size_t pos = cursor->pos; - size_t pos_match = text_bracket_match_except(win->text, pos, "<>"); - if (pos != pos_match && win->start <= pos_match && pos_match < win->end) { + size_t pos_match = text_bracket_match_except(view->text, pos, "<>"); + if (pos != pos_match && view->start <= pos_match && pos_match < view->end) { if (cursor->highlighted) - window_draw(win); /* clear active highlighting */ + view_draw(view); /* clear active highlighting */ cursor->pos = pos_match; - window_cursor_sync(win); + view_cursor_sync(view); cursor->line->cells[cursor->col].attr |= A_REVERSE; cursor->pos = pos; - window_cursor_sync(win); - win->ui->draw_text(win->ui, win->topline); + view_cursor_sync(view); + view->ui->draw_text(view->ui, view->topline); cursor->highlighted = true; } else if (cursor->highlighted) { cursor->highlighted = false; - window_draw(win); + view_draw(view); } } if (cursor->pos != cursor->lastpos) cursor->lastcol = 0; cursor->lastpos = cursor->pos; - if (win->ui) - win->ui->cursor_to(win->ui, cursor->col, cursor->row); + if (view->ui) + view->ui->cursor_to(view->ui, cursor->col, cursor->row); return cursor->pos; } /* move the cursor to the character at pos bytes from the begining of the file. - * if pos is not in the current viewport, redraw the window to make it visible */ -void window_cursor_to(Win *win, size_t pos) { - size_t max = text_size(win->text); + * if pos is not in the current viewport, redraw the view to make it visible */ +void view_cursor_to(View *view, size_t pos) { + size_t max = text_size(view->text); if (pos > max) pos = max > 0 ? max - 1 : 0; - if (pos == max && win->end != max) { - /* do not display an empty screen when showing the end of the file */ - win->start = max - 1; - window_viewport_up(win, win->height / 2); + if (pos == max && view->end != max) { + /* do not display an empty screen when shoviewg the end of the file */ + view->start = max - 1; + view_viewport_up(view, view->height / 2); } else { /* 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 window then simply start displaying text at the new + * available in the view then simply start displaying text at the new * cursor position */ - for (int i = 0; i < 2 && (pos < win->start || pos > win->end); i++) { - win->start = i == 0 ? text_line_begin(win->text, pos) : pos; - window_draw(win); + 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); } } - win->cursor.pos = pos; - window_cursor_sync(win); - window_cursor_update(win); + view->cursor.pos = pos; + view_cursor_sync(view); + view_cursor_update(view); } -/* redraw the complete with data starting from win->start bytes into the file. - * stop once the screen is full, update win->end, win->lastline */ -void window_draw(Win *win) { - window_clear(win); +/* redraw the complete with data starting from view->start bytes into the file. + * stop once the screen is full, update view->end, view->lastline */ +void view_draw(View *view) { + view_clear(view); /* current absolute file position */ - size_t pos = win->start; + size_t pos = view->start; /* number of bytes to read in one go */ - size_t text_len = win->width * win->height; + size_t text_len = view->width * view->height; /* current buffer to work with */ char text[text_len+1]; /* remaining bytes to process in buffer*/ - size_t rem = text_bytes_get(win->text, pos, text_len, text); + size_t rem = text_bytes_get(view->text, pos, text_len, text); /* NUL terminate because regex(3) function expect it */ text[rem] = '\0'; /* current position into buffer from which to interpret a character */ char *cur = text; /* current selection */ - Filerange sel = window_selection_get(win); + Filerange sel = view_selection_get(view); /* syntax definition to use */ - Syntax *syntax = win->syntax; + Syntax *syntax = view->syntax; /* matched tokens for each syntax rule */ regmatch_t match[syntax ? LENGTH(syntax->rules) : 1][1], *matched = NULL; memset(match, 0, sizeof match); @@ -403,7 +403,7 @@ void window_draw(Win *win) { /* within matched expression */ matched = &match[i][0]; attrs = rule->color->attr; - break; /* first match wins */ + break; /* first match views */ } } } @@ -421,7 +421,7 @@ void window_draw(Win *win) { * wide character. advance file position and read * another junk into buffer. */ - rem = text_bytes_get(win->text, pos, text_len, text); + rem = text_bytes_get(view->text, pos, text_len, text); text[rem] = '\0'; cur = text; continue; @@ -440,14 +440,14 @@ void window_draw(Win *win) { } if (cur[0] == '\r' && rem > 1 && cur[1] == '\n') { - /* convert windows style newline \r\n into a single char with len = 2 */ + /* convert views style newline \r\n into a single char with len = 2 */ cell = (Cell){ .data = "\n", .len = 2, .width = 1, .istab = false }; } cell.attr = attrs; if (sel.start <= pos && pos < sel.end) cell.attr |= A_REVERSE; - if (!window_addch(win, &cell)) + if (!view_addch(view, &cell)) break; rem -= cell.len; @@ -455,78 +455,78 @@ void window_draw(Win *win) { pos += cell.len; } - /* set end of viewing region */ - win->end = pos; - win->lastline = win->line ? win->line : win->bottomline; - win->lastline->next = NULL; - window_cursor_sync(win); - if (win->ui) - win->ui->draw_text(win->ui, win->topline); - if (sel.start != EPOS && win->events && win->events->selection) - win->events->selection(win->events->data, &sel); + /* set end of vieviewg region */ + view->end = pos; + view->lastline = view->line ? view->line : view->bottomline; + view->lastline->next = NULL; + view_cursor_sync(view); + if (view->ui) + view->ui->draw_text(view->ui, view->topline); + if (sel.start != EPOS && view->events && view->events->selection) + view->events->selection(view->events->data, &sel); } -bool window_resize(Win *win, int width, int height) { +bool view_resize(View *view, int width, int height) { size_t lines_size = height*(sizeof(Line) + width*sizeof(Cell)); - if (lines_size > win->lines_size) { - Line *lines = realloc(win->lines, lines_size); + if (lines_size > view->lines_size) { + Line *lines = realloc(view->lines, lines_size); if (!lines) return false; - win->lines = lines; - win->lines_size = lines_size; + view->lines = lines; + view->lines_size = lines_size; } - win->width = width; - win->height = height; - if (win->lines) - memset(win->lines, 0, win->lines_size); - window_draw(win); + view->width = width; + view->height = height; + if (view->lines) + memset(view->lines, 0, view->lines_size); + view_draw(view); return true; } -int window_height_get(Win *win) { - return win->height; +int view_height_get(View *view) { + return view->height; } -void window_free(Win *win) { - if (!win) +void view_free(View *view) { + if (!view) return; - free(win->lines); - free(win); + free(view->lines); + free(view); } -void window_reload(Win *win, Text *text) { - win->text = text; - window_selection_clear(win); - window_cursor_to(win, 0); - if (win->ui) - win->ui->reload(win->ui, text); +void view_reload(View *view, Text *text) { + view->text = text; + view_selection_clear(view); + view_cursor_to(view, 0); + if (view->ui) + view->ui->reload(view->ui, text); } -Win *window_new(Text *text, ViewEvent *events) { +View *view_new(Text *text, ViewEvent *events) { if (!text) return NULL; - Win *win = calloc(1, sizeof(Win)); - if (!win) + View *view = calloc(1, sizeof(View)); + if (!view) return NULL; - win->text = text; - win->events = events; - win->tabwidth = 8; + view->text = text; + view->events = events; + view->tabwidth = 8; - if (!window_resize(win, 1, 1)) { - window_free(win); + if (!view_resize(view, 1, 1)) { + view_free(view); return NULL; } - window_selection_clear(win); - window_cursor_to(win, 0); + view_selection_clear(view); + view_cursor_to(view, 0); - return win; + return view; } -void window_ui(Win *win, UiWin* ui) { - win->ui = ui; +void view_ui(View *view, UiWin* ui) { + view->ui = ui; } -size_t window_char_prev(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_char_prev(View *view) { + Cursor *cursor = &view->cursor; Line *line = cursor->line; do { @@ -534,7 +534,7 @@ size_t window_char_prev(Win *win) { if (!line->prev) return cursor->pos; cursor->line = line = line->prev; - cursor->col = MIN(line->width, win->width - 1); + cursor->col = MIN(line->width, view->width - 1); cursor->row--; } else { cursor->col--; @@ -542,16 +542,16 @@ size_t window_char_prev(Win *win) { } while (line->cells[cursor->col].len == 0); cursor->pos -= line->cells[cursor->col].len; - return window_cursor_update(win); + return view_cursor_update(view); } -size_t window_char_next(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_char_next(View *view) { + Cursor *cursor = &view->cursor; Line *line = cursor->line; do { cursor->pos += line->cells[cursor->col].len; - if ((line->width == win->width && cursor->col == win->width - 1) || + if ((line->width == view->width && cursor->col == view->width - 1) || cursor->col == line->width) { if (!line->next) return cursor->pos; @@ -563,15 +563,15 @@ size_t window_char_next(Win *win) { } } while (line->cells[cursor->col].len == 0); - return window_cursor_update(win); + return view_cursor_update(view); } -static size_t window_cursor_set(Win *win, Line *line, int col) { +static size_t view_cursor_set(View *view, Line *line, int col) { int row = 0; - size_t pos = win->start; - Cursor *cursor = &win->cursor; + size_t pos = view->start; + Cursor *cursor = &view->cursor; /* get row number and file offset at start of the given line */ - for (Line *cur = win->topline; cur && cur != line; cur = cur->next) { + for (Line *cur = view->topline; cur && cur != line; cur = cur->next) { pos += cur->len; row++; } @@ -591,35 +591,35 @@ static size_t window_cursor_set(Win *win, Line *line, int col) { cursor->pos = pos; cursor->line = line; - window_cursor_update(win); + view_cursor_update(view); return pos; } -static bool window_viewport_down(Win *win, int n) { +static bool view_viewport_down(View *view, int n) { Line *line; - if (win->end == text_size(win->text)) + if (view->end == text_size(view->text)) return false; - if (n >= win->height) { - win->start = win->end; + if (n >= view->height) { + view->start = view->end; } else { - for (line = win->topline; line && n > 0; line = line->next, n--) - win->start += line->len; + for (line = view->topline; line && n > 0; line = line->next, n--) + view->start += line->len; } - window_draw(win); + view_draw(view); return true; } -static bool window_viewport_up(Win *win, int n) { +static 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 */ - if (win->start == 0) + if (view->start == 0) return false; - size_t max = win->width * win->height; + size_t max = view->width * view->height; char c; - Iterator it = text_iterator_get(win->text, win->start - 1); + Iterator it = text_iterator_get(view->text, view->start - 1); if (!text_iterator_byte_get(&it, &c)) return false; @@ -637,262 +637,262 @@ static bool window_viewport_up(Win *win, int n) { } while (text_iterator_byte_prev(&it, &c)); if (c == '\r') off++; - win->start -= off; - window_draw(win); + view->start -= off; + view_draw(view); return true; } -void window_redraw_top(Win *win) { - Line *line = win->cursor.line; - for (Line *cur = win->topline; cur && cur != line; cur = cur->next) - win->start += cur->len; - window_draw(win); - window_cursor_to(win, win->cursor.pos); +void view_redraw_top(View *view) { + Line *line = view->cursor.line; + for (Line *cur = view->topline; cur && cur != line; cur = cur->next) + view->start += cur->len; + view_draw(view); + view_cursor_to(view, view->cursor.pos); } -void window_redraw_center(Win *win) { - int center = win->height / 2; - size_t pos = win->cursor.pos; +void view_redraw_center(View *view) { + int center = view->height / 2; + size_t pos = view->cursor.pos; for (int i = 0; i < 2; i++) { int linenr = 0; - Line *line = win->cursor.line; - for (Line *cur = win->topline; cur && cur != line; cur = cur->next) + Line *line = view->cursor.line; + for (Line *cur = view->topline; cur && cur != line; cur = cur->next) linenr++; if (linenr < center) { - window_slide_down(win, center - linenr); + view_slide_down(view, center - linenr); continue; } - for (Line *cur = win->topline; cur && cur != line && linenr > center; cur = cur->next) { - win->start += cur->len; + for (Line *cur = view->topline; cur && cur != line && linenr > center; cur = cur->next) { + view->start += cur->len; linenr--; } break; } - window_draw(win); - window_cursor_to(win, pos); + view_draw(view); + view_cursor_to(view, pos); } -void window_redraw_bottom(Win *win) { - Line *line = win->cursor.line; - if (line == win->lastline) +void view_redraw_bottom(View *view) { + Line *line = view->cursor.line; + if (line == view->lastline) return; int linenr = 0; - size_t pos = win->cursor.pos; - for (Line *cur = win->topline; cur && cur != line; cur = cur->next) + size_t pos = view->cursor.pos; + for (Line *cur = view->topline; cur && cur != line; cur = cur->next) linenr++; - window_slide_down(win, win->height - linenr - 1); - window_cursor_to(win, pos); + view_slide_down(view, view->height - linenr - 1); + view_cursor_to(view, pos); } -size_t window_slide_up(Win *win, int lines) { - Cursor *cursor = &win->cursor; - if (window_viewport_down(win, lines)) { - if (cursor->line == win->topline) - window_cursor_set(win, win->topline, cursor->col); +size_t view_slide_up(View *view, int lines) { + Cursor *cursor = &view->cursor; + if (view_viewport_down(view, lines)) { + if (cursor->line == view->topline) + view_cursor_set(view, view->topline, cursor->col); else - window_cursor_to(win, cursor->pos); + view_cursor_to(view, cursor->pos); } else { - window_screenline_down(win); + view_screenline_down(view); } return cursor->pos; } -size_t window_slide_down(Win *win, int lines) { - Cursor *cursor = &win->cursor; - if (window_viewport_up(win, lines)) { - if (cursor->line == win->lastline) - window_cursor_set(win, win->lastline, cursor->col); +size_t view_slide_down(View *view, int lines) { + Cursor *cursor = &view->cursor; + if (view_viewport_up(view, lines)) { + if (cursor->line == view->lastline) + view_cursor_set(view, view->lastline, cursor->col); else - window_cursor_to(win, cursor->pos); + view_cursor_to(view, cursor->pos); } else { - window_screenline_up(win); + view_screenline_up(view); } return cursor->pos; } -size_t window_scroll_up(Win *win, int lines) { - Cursor *cursor = &win->cursor; - if (window_viewport_up(win, lines)) { - Line *line = cursor->line < win->lastline ? cursor->line : win->lastline; - window_cursor_set(win, line, win->cursor.col); +size_t view_scroll_up(View *view, int lines) { + Cursor *cursor = &view->cursor; + if (view_viewport_up(view, lines)) { + Line *line = cursor->line < view->lastline ? cursor->line : view->lastline; + view_cursor_set(view, line, view->cursor.col); } else { - window_cursor_to(win, 0); + view_cursor_to(view, 0); } return cursor->pos; } -size_t window_scroll_down(Win *win, int lines) { - Cursor *cursor = &win->cursor; - if (window_viewport_down(win, lines)) { - Line *line = cursor->line > win->topline ? cursor->line : win->topline; - window_cursor_set(win, line, cursor->col); +size_t view_scroll_down(View *view, int lines) { + Cursor *cursor = &view->cursor; + if (view_viewport_down(view, lines)) { + Line *line = cursor->line > view->topline ? cursor->line : view->topline; + view_cursor_set(view, line, cursor->col); } else { - window_cursor_to(win, text_size(win->text)); + view_cursor_to(view, text_size(view->text)); } return cursor->pos; } -size_t window_line_up(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_line_up(View *view) { + Cursor *cursor = &view->cursor; if (cursor->line->prev && cursor->line->prev->prev && cursor->line->lineno != cursor->line->prev->lineno && cursor->line->prev->lineno != cursor->line->prev->prev->lineno) - return window_screenline_up(win); - size_t bol = text_line_begin(win->text, cursor->pos); - size_t prev = text_line_prev(win->text, bol); - size_t pos = text_line_offset(win->text, prev, cursor->pos - bol); - window_cursor_to(win, pos); + return view_screenline_up(view); + size_t bol = text_line_begin(view->text, cursor->pos); + size_t prev = text_line_prev(view->text, bol); + size_t pos = text_line_offset(view->text, prev, cursor->pos - bol); + view_cursor_to(view, pos); return cursor->pos; } -size_t window_line_down(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_line_down(View *view) { + Cursor *cursor = &view->cursor; if (!cursor->line->next || cursor->line->next->lineno != cursor->line->lineno) - return window_screenline_down(win); - size_t bol = text_line_begin(win->text, cursor->pos); - size_t next = text_line_next(win->text, bol); - size_t pos = text_line_offset(win->text, next, cursor->pos - bol); - window_cursor_to(win, pos); + return view_screenline_down(view); + size_t bol = text_line_begin(view->text, cursor->pos); + size_t next = text_line_next(view->text, bol); + size_t pos = text_line_offset(view->text, next, cursor->pos - bol); + view_cursor_to(view, pos); return cursor->pos; } -size_t window_screenline_up(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_screenline_up(View *view) { + Cursor *cursor = &view->cursor; int lastcol = cursor->lastcol; if (!lastcol) lastcol = cursor->col; if (!cursor->line->prev) - window_scroll_up(win, 1); + view_scroll_up(view, 1); if (cursor->line->prev) - window_cursor_set(win, cursor->line->prev, lastcol); + view_cursor_set(view, cursor->line->prev, lastcol); cursor->lastcol = lastcol; return cursor->pos; } -size_t window_screenline_down(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_screenline_down(View *view) { + Cursor *cursor = &view->cursor; int lastcol = cursor->lastcol; if (!lastcol) lastcol = cursor->col; - if (!cursor->line->next && cursor->line == win->bottomline) - window_scroll_down(win, 1); + if (!cursor->line->next && cursor->line == view->bottomline) + view_scroll_down(view, 1); if (cursor->line->next) - window_cursor_set(win, cursor->line->next, lastcol); + view_cursor_set(view, cursor->line->next, lastcol); cursor->lastcol = lastcol; return cursor->pos; } -size_t window_screenline_begin(Win *win) { - return window_cursor_set(win, win->cursor.line, 0); +size_t view_screenline_begin(View *view) { + return view_cursor_set(view, view->cursor.line, 0); } -size_t window_screenline_middle(Win *win) { - Cursor *cursor = &win->cursor; - return window_cursor_set(win, cursor->line, cursor->line->width / 2); +size_t view_screenline_middle(View *view) { + Cursor *cursor = &view->cursor; + return view_cursor_set(view, cursor->line, cursor->line->width / 2); } -size_t window_screenline_end(Win *win) { - Cursor *cursor = &win->cursor; +size_t view_screenline_end(View *view) { + Cursor *cursor = &view->cursor; int col = cursor->line->width - 1; - return window_cursor_set(win, cursor->line, col >= 0 ? col : 0); + return view_cursor_set(view, cursor->line, col >= 0 ? col : 0); } -size_t window_delete_key(Win *win) { - Cursor *cursor = &win->cursor; +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(win->text, cursor->pos, len); - window_draw(win); - window_cursor_to(win, cursor->pos); + text_delete(view->text, cursor->pos, len); + view_draw(view); + view_cursor_to(view, cursor->pos); return cursor->pos; } -size_t window_backspace_key(Win *win) { - Cursor *cursor = &win->cursor; - if (win->start == cursor->pos) { - if (win->start == 0) +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 window + /* 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; - window_viewport_up(win, 1); - window_cursor_to(win, pos); + view_viewport_up(view, 1); + view_cursor_to(view, pos); } - window_char_prev(win); + view_char_prev(view); size_t pos = cursor->pos; size_t len = cursor->line->cells[cursor->col].len; - text_delete(win->text, pos, len); - window_draw(win); - window_cursor_to(win, pos); + text_delete(view->text, pos, len); + view_draw(view); + view_cursor_to(view, pos); return pos; } -size_t window_insert_key(Win *win, const char *c, size_t len) { - size_t pos = win->cursor.pos; - text_insert(win->text, pos, c, len); - if (win->cursor.line == win->bottomline && memchr(c, '\n', len)) - window_viewport_down(win, 1); +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 - window_draw(win); + view_draw(view); pos += len; - window_cursor_to(win, pos); + view_cursor_to(view, pos); return pos; } -size_t window_replace_key(Win *win, const char *c, size_t len) { - Cursor *cursor = &win->cursor; +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(win->text, pos, oldlen); + text_delete(view->text, pos, oldlen); } - text_insert(win->text, pos, c, len); - if (cursor->line == win->bottomline && memchr(c, '\n', len)) - window_viewport_down(win, 1); + text_insert(view->text, pos, c, len); + if (cursor->line == view->bottomline && memchr(c, '\n', len)) + view_viewport_down(view, 1); else - window_draw(win); + view_draw(view); pos += len; - window_cursor_to(win, pos); + view_cursor_to(view, pos); return pos; } -size_t window_cursor_get(Win *win) { - return win->cursor.pos; +size_t view_cursor_get(View *view) { + return view->cursor.pos; } -const Line *window_lines_get(Win *win) { - return win->topline; +const Line *view_lines_get(View *view) { + return view->topline; } -void window_scroll_to(Win *win, size_t pos) { - while (pos < win->start && window_viewport_up(win, 1)); - while (pos > win->end && window_viewport_down(win, 1)); - window_cursor_to(win, pos); +void view_scroll_to(View *view, size_t pos) { + while (pos < view->start && view_viewport_up(view, 1)); + while (pos > view->end && view_viewport_down(view, 1)); + view_cursor_to(view, pos); } -void window_selection_start(Win *win) { - if (win->sel.start != EPOS && win->sel.end != EPOS) +void view_selection_start(View *view) { + if (view->sel.start != EPOS && view->sel.end != EPOS) return; - size_t pos = window_cursor_get(win); - win->sel.start = win->sel.end = pos; - window_draw(win); - window_cursor_to(win, pos); + size_t pos = view_cursor_get(view); + view->sel.start = view->sel.end = pos; + view_draw(view); + view_cursor_to(view, pos); } -void window_syntax_set(Win *win, Syntax *syntax) { - win->syntax = syntax; +void view_syntax_set(View *view, Syntax *syntax) { + view->syntax = syntax; } -Syntax *window_syntax_get(Win *win) { - return win->syntax; +Syntax *view_syntax_get(View *view) { + return view->syntax; } -size_t window_screenline_goto(Win *win, int n) { - size_t pos = win->start; - for (Line *line = win->topline; --n > 0 && line != win->lastline; line = line->next) +size_t view_screenline_goto(View *view, int n) { + size_t pos = view->start; + for (Line *line = view->topline; --n > 0 && line != view->lastline; line = line->next) pos += line->len; return pos; } |
