Skip to content

Commit

Permalink
vis: implement :set cursorline
Browse files Browse the repository at this point in the history
martanne committed Nov 8, 2015
1 parent c339bb3 commit 87c609e
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lexers/themes/solarized.lua
Original file line number Diff line number Diff line change
@@ -50,5 +50,6 @@ lexers.STYLE_IDENTIFIER = fg

lexers.STYLE_LINENUMBER = fg
lexers.STYLE_CURSOR = 'fore:'..colors.base03..',back:'..colors.base0
lexers.STYLE_CURSOR_LINE = 'back:'..colors.base02
-- lexers.STYLE_SELECTION = 'back:'..colors.base02
lexers.STYLE_SELECTION = 'back:white'
13 changes: 13 additions & 0 deletions ui-curses.c
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
#include "ui.h"
#include "ui-curses.h"
#include "util.h"
#include "text-util.h"

#ifdef NCURSES_VERSION
# ifndef NCURSES_EXT_COLORS
@@ -649,9 +650,18 @@ static void ui_window_draw(UiWin *w) {
wmove(win->win, 0, 0);
int width = view_width_get(win->view);
CellStyle *prev_style = NULL;
size_t cursor_lineno = -1;
if (win->options & UI_OPTION_CURSOR_LINE && win->ui->selwin == win) {
Cursor *cursor = view_cursors(win->view);
Filerange selection = view_cursors_selection_get(cursor);
if (!view_cursors_next(cursor) && !text_range_valid(&selection))
cursor_lineno = view_cursor_getpos(win->view).line;
}
short selection_bg = win->styles[UI_STYLE_SELECTION].bg;
short cursor_line_bg = win->styles[UI_STYLE_CURSOR_LINE].bg;
attr_t attr;
for (const Line *l = view_lines_get(win->view); l; l = l->next) {
bool cursor_line = l->lineno == cursor_lineno;
for (int x = 0; x < width; x++) {
CellStyle *style = &win->styles[l->cells[x].attr];
if (l->cells[x].cursor && (win->ui->selwin == win || win->ui->prompt_win == win)) {
@@ -660,6 +670,9 @@ static void ui_window_draw(UiWin *w) {
} else if (l->cells[x].selected) {
attr = style->attr | COLOR_PAIR(color_pair_get(style->fg, selection_bg));
prev_style = NULL;
} else if (cursor_line) {
attr = style->attr | COLOR_PAIR(color_pair_get(style->fg, cursor_line_bg));
prev_style = NULL;
} else if (style != prev_style) {
attr = style_to_attr(style);
prev_style = style;
2 changes: 2 additions & 0 deletions ui.h
Original file line number Diff line number Diff line change
@@ -21,12 +21,14 @@ enum UiOption {
UI_OPTION_SYMBOL_TAB_FILL = 1 << 4,
UI_OPTION_SYMBOL_EOL = 1 << 5,
UI_OPTION_SYMBOL_EOF = 1 << 6,
UI_OPTION_CURSOR_LINE = 1 << 7,
};

enum UiStyles {
UI_STYLE_LEXER_MAX = 64,
UI_STYLE_DEFAULT,
UI_STYLE_CURSOR,
UI_STYLE_CURSOR_LINE,
UI_STYLE_SELECTION,
UI_STYLE_LINENUMBER,
UI_STYLE_MAX,
3 changes: 3 additions & 0 deletions view.c
Original file line number Diff line number Diff line change
@@ -889,6 +889,9 @@ bool view_syntax_set(View *view, const char *name) {
lua_getfield(L, -1, "STYLE_CURSOR");
view->ui->syntax_style(view->ui, UI_STYLE_CURSOR, lua_tostring(L, -1));
lua_pop(L, 1);
lua_getfield(L, -1, "STYLE_CURSOR_LINE");
view->ui->syntax_style(view->ui, UI_STYLE_CURSOR_LINE, lua_tostring(L, -1));
lua_pop(L, 1);
lua_getfield(L, -1, "STYLE_SELECTION");
view->ui->syntax_style(view->ui, UI_STYLE_SELECTION, lua_tostring(L, -1));
lua_pop(L, 1);
11 changes: 11 additions & 0 deletions vis-cmds.c
Original file line number Diff line number Diff line change
@@ -158,6 +158,7 @@ static bool cmd_set(Vis *vis, Filerange *range, enum CmdOpt cmdopt, const char *
OPTION_SHOW,
OPTION_NUMBER,
OPTION_NUMBER_RELATIVE,
OPTION_CURSOR_LINE,
};

/* definitions have to be in the same order as the enum above */
@@ -169,6 +170,7 @@ static bool cmd_set(Vis *vis, Filerange *range, enum CmdOpt cmdopt, const char *
[OPTION_SHOW] = { { "show" }, OPTION_TYPE_STRING },
[OPTION_NUMBER] = { { "numbers", "nu" }, OPTION_TYPE_BOOL },
[OPTION_NUMBER_RELATIVE] = { { "relativenumbers", "rnu" }, OPTION_TYPE_BOOL },
[OPTION_CURSOR_LINE] = { { "cursorline", "cul" }, OPTION_TYPE_BOOL },
};

if (!vis->options) {
@@ -313,6 +315,15 @@ static bool cmd_set(Vis *vis, Filerange *range, enum CmdOpt cmdopt, const char *
view_options_set(vis->win->view, opt);
break;
}
case OPTION_CURSOR_LINE: {
enum UiOption opt = view_options_get(vis->win->view);
if (arg.b)
opt |= UI_OPTION_CURSOR_LINE;
else
opt &= ~UI_OPTION_CURSOR_LINE;
view_options_set(vis->win->view, opt);
break;
}
}

return true;

0 comments on commit 87c609e

Please sign in to comment.