From e29bbbb8ea4f59d277a6e4f50d7d2501ddd04b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 20 May 2016 13:16:46 +0200 Subject: [PATCH] view: add functions to style a file range --- view.c | 35 +++++++++++++++++++++++++++++++++++ view.h | 3 +++ 2 files changed, 38 insertions(+) diff --git a/view.c b/view.c index 14043ee01..50b7c8ecf 100644 --- a/view.c +++ b/view.c @@ -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)); +} diff --git a/view.h b/view.h index c44726ed5..1b2ea4e5d 100644 --- a/view.h +++ b/view.h @@ -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