vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
commit 4b184f462a27379cf2690e564190023f14c70ed7 parent eb2e55cca9791c840e81e8a309db2d1714140ec4 Author: Marc André Tanner <mat@brain-dump.org> Date: Fri, 3 Apr 2015 18:39:22 +0200 Add option to display relative line numbers :set rnu Based on a patch by Sebastian Götte. Diffstat:
| M | README | | | 6 | ++++-- |
| M | ui-curses.c | | | 16 | ++++++++++++++-- |
| M | ui.h | | | 5 | +++-- |
| M | vis.c | | | 6 | ++++++ |
| M | window.c | | | 4 | ++++ |
| M | window.h | | | 4 | ++-- |
6 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/README b/README @@ -449,9 +449,11 @@ and their current support in vis. replicate spaces and tabs at the beginning of the line when starting a new line. - number (yes|no) + number (yes|no) + relativenumber (yes|no) - whether line numbers are printed alongside the file content + whether absolute or relative line numbers are printed alongside + the file content syntax name diff --git a/ui-curses.c b/ui-curses.c @@ -197,10 +197,19 @@ static void ui_window_draw_sidebar(UiCursesWin *win, const Line *line) { } else { int i = 0; size_t prev_lineno = 0; + size_t cursor_lineno = window_cursor_getpos(win->view).line; werase(win->winside); for (const Line *l = line; l; l = l->next, i++) { - if (l->lineno != prev_lineno) - mvwprintw(win->winside, i, 0, "%*u", sidebar_width-1, l->lineno); + if (l->lineno != prev_lineno) { + if (win->options & UI_OPTION_LINE_NUMBERS_ABSOLUTE) { + mvwprintw(win->winside, i, 0, "%*u", sidebar_width-1, l->lineno); + } else if (win->options & UI_OPTION_LINE_NUMBERS_RELATIVE) { + size_t rel = l->lineno > cursor_lineno ? + l->lineno - cursor_lineno : + cursor_lineno - l->lineno; + mvwprintw(win->winside, i, 0, "%*u", sidebar_width-1, rel); + } + } prev_lineno = l->lineno; } mvwvline(win->winside, 0, sidebar_width-1, ACS_VLINE, win->height-1); @@ -338,6 +347,8 @@ static void ui_window_cursor_to(UiWin *w, int x, int y) { UiCursesWin *win = (UiCursesWin*)w; wmove(win->win, y, x); ui_window_draw_status(w); + if (win->options & UI_OPTION_LINE_NUMBERS_RELATIVE) + ui_window_draw_sidebar(win, window_lines_get(win->view)); } static void ui_window_draw_text(UiWin *w, const Line *line) { @@ -386,6 +397,7 @@ static void ui_window_options(UiWin *w, enum UiOption options) { } break; case UI_OPTION_LINE_NUMBERS_ABSOLUTE: + case UI_OPTION_LINE_NUMBERS_RELATIVE: if (!win->winside) win->winside = newwin(1, 1, 1, 1); break; diff --git a/ui.h b/ui.h @@ -10,8 +10,9 @@ enum UiLayout { }; enum UiOption { - UI_OPTION_LINE_NUMBERS_NONE, - UI_OPTION_LINE_NUMBERS_ABSOLUTE, + UI_OPTION_LINE_NUMBERS_NONE = 0, + UI_OPTION_LINE_NUMBERS_ABSOLUTE = 1 << 0, + UI_OPTION_LINE_NUMBERS_RELATIVE = 1 << 1, }; #include <stdbool.h> diff --git a/vis.c b/vis.c @@ -1377,6 +1377,7 @@ static bool cmd_set(Filerange *range, enum CmdOpt cmdopt, const char *argv[]) { OPTION_TABWIDTH, OPTION_SYNTAX, OPTION_NUMBER, + OPTION_NUMBER_RELATIVE, }; static OptionDef options[] = { @@ -1385,6 +1386,7 @@ static bool cmd_set(Filerange *range, enum CmdOpt cmdopt, const char *argv[]) { [OPTION_TABWIDTH] = { "^(tabwidth|tw)$", OPTION_TYPE_NUMBER }, [OPTION_SYNTAX] = { "^(syntax|syn?)$", OPTION_TYPE_STRING }, [OPTION_NUMBER] = { "^(numbers?|nu)$", OPTION_TYPE_BOOL }, + [OPTION_NUMBER_RELATIVE] = { "^(relativenumbers?|rnu)$", OPTION_TYPE_BOOL }, }; static bool init = false; @@ -1474,6 +1476,10 @@ static bool cmd_set(Filerange *range, enum CmdOpt cmdopt, const char *argv[]) { editor_window_options(vis->win, arg.b ? UI_OPTION_LINE_NUMBERS_ABSOLUTE : UI_OPTION_LINE_NUMBERS_NONE); break; + case OPTION_NUMBER_RELATIVE: + editor_window_options(vis->win, arg.b ? UI_OPTION_LINE_NUMBERS_RELATIVE : + UI_OPTION_LINE_NUMBERS_NONE); + break; } return true; diff --git a/window.c b/window.c @@ -820,6 +820,10 @@ size_t window_cursor_get(Win *win) { return win->cursor.pos; } +const Line *window_lines_get(Win *win) { + return win->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)); diff --git a/window.h b/window.h @@ -76,6 +76,8 @@ size_t window_screenline_goto(Win*, int n); /* get cursor position in bytes from start of the file */ size_t window_cursor_get(Win*); + +const Line *window_lines_get(Win*); /* get cursor position in terms of screen coordinates */ CursorPos window_cursor_getpos(Win*); /* moves window viewport in direction until pos is visible. should only be @@ -103,7 +105,5 @@ Filerange window_viewport_get(Win*); /* associate a set of syntax highlighting rules to this window. */ void window_syntax_set(Win*, Syntax*); Syntax *window_syntax_get(Win*); -/* register a user defined function which will be called whenever the cursor has moved */ -void window_cursor_watch(Win *win, void (*cursor_moved)(Win*, void*), void *data); #endif