diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-07-21 17:13:58 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-07-21 18:01:58 +0200 |
| commit | 679552d6db9771ddbf29bd02eb8392b3f319d4cb (patch) | |
| tree | 2af606eb8fe1423908366313cdd5fbcdc9c46f45 | |
| parent | 38f00e3e8a50e1690dcb78cf1eca8b6befb7173b (diff) | |
| download | vis-679552d6db9771ddbf29bd02eb8392b3f319d4cb.tar.gz vis-679552d6db9771ddbf29bd02eb8392b3f319d4cb.tar.xz | |
ui: further separate curses related user interface code
By now ui-curses.[hc] are the only files dealing directly with
curses related functions. Integration of a proper mainloop is
still pending.
| -rw-r--r-- | config.def.h | 24 | ||||
| -rw-r--r-- | editor.c | 8 | ||||
| -rw-r--r-- | editor.h | 5 | ||||
| -rw-r--r-- | ui-curses.c | 19 | ||||
| -rw-r--r-- | ui-curses.h | 22 | ||||
| -rw-r--r-- | ui.h | 1 | ||||
| -rw-r--r-- | view.c | 10 | ||||
| -rw-r--r-- | view.h | 3 | ||||
| -rw-r--r-- | vis.c | 4 |
9 files changed, 60 insertions, 36 deletions
diff --git a/config.def.h b/config.def.h index 916831e..3007b60 100644 --- a/config.def.h +++ b/config.def.h @@ -830,18 +830,18 @@ enum { }; static Color colors[] = { - [COLOR_NOHILIT] = { .fg = -1, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX0] = { .fg = COLOR_RED, .bg = -1, .attr = A_BOLD }, - [COLOR_SYNTAX1] = { .fg = COLOR_GREEN, .bg = -1, .attr = A_BOLD }, - [COLOR_SYNTAX2] = { .fg = COLOR_GREEN, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX3] = { .fg = COLOR_MAGENTA, .bg = -1, .attr = A_BOLD }, - [COLOR_SYNTAX4] = { .fg = COLOR_MAGENTA, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX5] = { .fg = COLOR_BLUE, .bg = -1, .attr = A_BOLD }, - [COLOR_SYNTAX6] = { .fg = COLOR_RED, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX7] = { .fg = COLOR_BLUE, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX8] = { .fg = COLOR_CYAN, .bg = -1, .attr = A_NORMAL }, - [COLOR_SYNTAX9] = { .fg = COLOR_YELLOW, .bg = -1, .attr = A_NORMAL }, - { /* empty last element, array terminator */ } + [COLOR_NOHILIT] = { .fg = UI_COLOR_DEFAULT, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX0] = { .fg = UI_COLOR_RED, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_BOLD }, + [COLOR_SYNTAX1] = { .fg = UI_COLOR_GREEN, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_BOLD }, + [COLOR_SYNTAX2] = { .fg = UI_COLOR_GREEN, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX3] = { .fg = UI_COLOR_MAGENTA, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_BOLD }, + [COLOR_SYNTAX4] = { .fg = UI_COLOR_MAGENTA, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX5] = { .fg = UI_COLOR_BLUE, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_BOLD }, + [COLOR_SYNTAX6] = { .fg = UI_COLOR_RED, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX7] = { .fg = UI_COLOR_BLUE, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX8] = { .fg = UI_COLOR_CYAN, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + [COLOR_SYNTAX9] = { .fg = UI_COLOR_YELLOW, .bg = UI_COLOR_DEFAULT, .attr = UI_ATTR_NORMAL }, + { /* empty last element, array terminator */ } }; /* Syntax color definitions per file type. Each rule consists of a regular @@ -178,16 +178,10 @@ void editor_tabwidth_set(Editor *ed, int tabwidth) { ed->tabwidth = tabwidth; } -bool editor_syntax_load(Editor *ed, Syntax *syntaxes, Color *colors) { +bool editor_syntax_load(Editor *ed, Syntax *syntaxes) { bool success = true; ed->syntaxes = syntaxes; - for (Color *color = colors; color && color->fg; color++) { - if (color->attr == 0) - color->attr = A_NORMAL; - color->attr |= COLOR_PAIR(ed->ui->color_get(color->fg, color->bg)); - } - for (Syntax *syn = syntaxes; syn && syn->name; syn++) { if (regcomp(&syn->file_regex, syn->file, REG_EXTENDED|REG_NOSUB|REG_ICASE|REG_NEWLINE)) success = false; @@ -1,7 +1,6 @@ #ifndef EDITOR_H #define EDITOR_H -#include <curses.h> #include <signal.h> #include <stddef.h> #include <stdbool.h> @@ -273,9 +272,9 @@ int editor_tabwidth_get(Editor*); /* load a set of syntax highlighting definitions which will be associated * to the underlying window based on the file type loaded. * - * both *syntaxes and *colors must point to a NULL terminated arrays. + * The parameter `syntaxes' has to point to a NULL terminated array. */ -bool editor_syntax_load(Editor*, Syntax *syntaxes, Color *colors); +bool editor_syntax_load(Editor*, Syntax *syntaxes); void editor_syntax_unload(Editor*); /* creates a new window, and loads the given file. if filename is NULL diff --git a/ui-curses.c b/ui-curses.c index 2473521..283ee16 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -371,11 +371,17 @@ static void ui_window_draw_text(UiWin *w, const Line *line) { /* add a single space in an otherwise empty line to make * the selection cohorent */ if (l->width == 1 && l->cells[0].data[0] == '\n') { - wattrset(win->win, l->cells[0].attr); + int attr = l->cells[0].attr; + if (l->cells[0].selected) + attr |= A_REVERSE; + wattrset(win->win, attr); waddstr(win->win, " \n"); } else { for (int x = 0; x < l->width; x++) { - wattrset(win->win, l->cells[x].attr); + int attr = l->cells[x].attr; + if (l->cells[x].selected) + attr |= A_REVERSE; + wattrset(win->win, attr); waddstr(win->win, l->cells[x].data); } if (l->width != win->width - win->sidebar_width) @@ -589,7 +595,7 @@ static void ui_terminal_restore(Ui *ui) { wclear(stdscr); } -Ui *ui_curses_new(void) { +Ui *ui_curses_new(Color *colors) { setlocale(LC_CTYPE, ""); if (!getenv("ESCDELAY")) set_escdelay(50); @@ -631,13 +637,18 @@ Ui *ui_curses_new(void) { .arrange = arrange, .info = info, .info_hide = info_hide, - .color_get = color_get, .haskey = ui_haskey, .getkey = ui_getkey, .terminal_save = ui_terminal_save, .terminal_restore = ui_terminal_restore, }; + for (Color *color = colors; color && color->fg; color++) { + if (color->attr == 0) + color->attr = A_NORMAL; + color->attr |= COLOR_PAIR(color_get(color->fg, color->bg)); + } + struct sigaction sa; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); diff --git a/ui-curses.h b/ui-curses.h index 0509f25..32cdf2f 100644 --- a/ui-curses.h +++ b/ui-curses.h @@ -3,6 +3,7 @@ #include <curses.h> #include "ui.h" +#include "syntax.h" enum { UI_KEY_DOWN = KEY_DOWN, @@ -20,7 +21,26 @@ enum { UI_KEY_SHIFT_RIGHT = KEY_SRIGHT, }; -Ui *ui_curses_new(void); +enum { + UI_COLOR_DEFAULT = -1, + UI_COLOR_BLACK = COLOR_BLACK, + UI_COLOR_RED = COLOR_RED, + UI_COLOR_GREEN = COLOR_GREEN, + UI_COLOR_YELLOW = COLOR_YELLOW, + UI_COLOR_BLUE = COLOR_BLUE, + UI_COLOR_MAGENTA = COLOR_MAGENTA, + UI_COLOR_CYAN = COLOR_CYAN, + UI_COLOR_WHITE = COLOR_WHITE, +}; + +enum { + UI_ATTR_NORMAL = A_NORMAL, + UI_ATTR_UNDERLINE = A_UNDERLINE, + UI_ATTR_REVERSE = A_REVERSE, + UI_ATTR_BOLD = A_BOLD, +}; + +Ui *ui_curses_new(Color *colors); void ui_curses_free(Ui*); #endif @@ -29,7 +29,6 @@ typedef struct { struct Ui { bool (*init)(Ui*, Editor*); void (*free)(Ui*); - short (*color_get)(short fg, short bg); void (*resize)(Ui*); UiWin* (*window_new)(Ui*, View*, File*); void (*window_free)(UiWin*); @@ -167,7 +167,7 @@ static bool view_addch(View *view, Cell *cell) { int t = w == 0 ? SYNTAX_SYMBOL_TAB : SYNTAX_SYMBOL_TAB_FILL; strncpy(cell->data, view->symbols[t]->symbol, sizeof(cell->data)-1); if (view->symbols[t]->color) - cell->attr = view->symbols[t]->color->attr | (cell->attr & A_REVERSE); + cell->attr = view->symbols[t]->color->attr; view->line->cells[view->col] = *cell; view->line->len += cell->len; view->line->width += cell->width; @@ -215,7 +215,7 @@ static bool view_addch(View *view, Cell *cell) { if (cell->data[0] == ' ') { strncpy(cell->data, view->symbols[SYNTAX_SYMBOL_SPACE]->symbol, sizeof(cell->data)-1); if (view->symbols[SYNTAX_SYMBOL_SPACE]->color) - cell->attr = view->symbols[SYNTAX_SYMBOL_SPACE]->color->attr | (cell->attr & A_REVERSE); + cell->attr = view->symbols[SYNTAX_SYMBOL_SPACE]->color->attr; } @@ -304,7 +304,7 @@ static size_t view_cursor_update(View *view) { view_draw(view); /* clear active highlighting */ cursor->pos = pos_match; view_cursor_sync(view); - cursor->line->cells[cursor->col].attr |= A_REVERSE; + cursor->line->cells[cursor->col].selected = true; cursor->pos = pos; view_cursor_sync(view); view->ui->draw_text(view->ui, view->topline); @@ -374,7 +374,7 @@ void view_draw(View *view) { regmatch_t match[syntax ? LENGTH(syntax->rules) : 1][1], *matched = NULL; memset(match, 0, sizeof match); /* default and current curses attributes to use */ - int default_attrs = COLOR_PAIR(0) | A_NORMAL, attrs = default_attrs; + int default_attrs = 0, attrs = default_attrs; /* start from known multibyte state */ mbstate_t mbstate = { 0 }; @@ -468,7 +468,7 @@ void view_draw(View *view) { cell.attr = attrs; if (sel.start <= pos && pos < sel.end) - cell.attr |= A_REVERSE; + cell.selected = true; if (!view_addch(view, &cell)) break; @@ -22,8 +22,9 @@ typedef struct { occupied by the same character have a length of 0. */ char data[8]; /* utf8 encoded character displayed in this cell might not be the the same as in the underlying text, for example tabs get expanded */ - bool istab; unsigned int attr; + bool istab; + bool selected; } Cell; typedef struct Line Line; @@ -2453,12 +2453,12 @@ int main(int argc, char *argv[]) { } } - if (!(vis = editor_new(ui_curses_new()))) + if (!(vis = editor_new(ui_curses_new(colors)))) die("Could not allocate editor core\n"); vis->mode_prev = vis->mode = config->mode; - if (!editor_syntax_load(vis, syntaxes, colors)) + if (!editor_syntax_load(vis, syntaxes)) die("Could not load syntax highlighting definitions\n"); char *cmd = NULL; |
