Skip to content

Commit

Permalink
view: add functions to style a file range
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed May 21, 2016
1 parent 207106f commit e29bbbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1510,3 +1510,38 @@ void view_selections_set(Selection *s, const Filerange *r) {
Text *view_text(View *view) {
return view->text;
}

bool view_style_define(View *view, enum UiStyle id, const char *style) {
return view->ui->syntax_style(view->ui, id, style);
}

void view_style(View *view, enum UiStyle style, size_t start, size_t end) {
if (end < view->start || start > view->end)
return;

size_t pos = view->start;
Line *line = view->topline;

/* skip lines before range to be styled */
while (line && pos + line->len <= start) {
pos += line->len;
line = line->next;
}

if (!line)
return;

int col = 0, width = view->width;

/* skip columns before range to be styled */
while (pos < start && col < width)
pos += line->cells[col++].len;

do {
while (pos <= end && col < width) {
pos += line->cells[col].len;
line->cells[col++].style = style;
}
col = 0;
} while (pos <= end && (line = line->next));
}
3 changes: 3 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,7 @@ Cursor *view_cursors_column(View*, int column);
/* get next cursor (i.e. on another line) in zero based column */
Cursor *view_cursors_column_next(Cursor*, int column);

bool view_style_define(View*, enum UiStyle, const char *style);
void view_style(View*, enum UiStyle, size_t start, size_t end);

#endif

0 comments on commit e29bbbb

Please sign in to comment.