Skip to content

Commit 4a5fa53

Browse files
committed
use terminal's cursor and add STYLE_CURSOR_MATCHING style
Previously, vis used to hide the terminal's cursor, but now terminal's cursor is positioned to the active cursor's position. The STYLE_CURSOR_MATCHING style is added so that it can be adjusted independently.
1 parent 6f537f3 commit 4a5fa53

10 files changed

Lines changed: 37 additions & 20 deletions

File tree

lua/themes/base-16.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ lexers.STYLE_LINENUMBER = ''
2828
lexers.STYLE_LINENUMBER_CURSOR = lexers.STYLE_LINENUMBER
2929
lexers.STYLE_CURSOR = 'back:white,fore:black'
3030
lexers.STYLE_CURSOR_PRIMARY = lexers.STYLE_CURSOR..',fore:yellow'
31+
lexers.STYLE_CURSOR_MATCHING = lexers.STYLE_CURSOR..',fore:yellow'
3132
lexers.STYLE_CURSOR_LINE = 'underlined'
3233
lexers.STYLE_COLOR_COLUMN = 'back:red'
3334
lexers.STYLE_SELECTION = 'back:white,bold'

lua/themes/solarized.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ lexers.STYLE_LINENUMBER = 'fore:'..colors.base00..',back:'..colors.base02
5858
lexers.STYLE_LINENUMBER_CURSOR = 'back:'..colors.base00..',fore:'..colors.base02
5959
lexers.STYLE_CURSOR = 'fore:'..colors.base03..',back:'..colors.base0
6060
lexers.STYLE_CURSOR_PRIMARY = lexers.STYLE_CURSOR..',back:yellow'
61+
lexers.STYLE_CURSOR_MATCHING = lexers.STYLE_CURSOR..',back:yellow'
6162
lexers.STYLE_CURSOR_LINE = 'back:'..colors.base02
6263
lexers.STYLE_COLOR_COLUMN = 'back:'..colors.base02
6364
-- lexers.STYLE_SELECTION = 'back:'..colors.base02

lua/themes/zenburn.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ lexers.STYLE_LINENUMBER = 'fore:#585858'
2929
lexers.STYLE_LINENUMBER_CURSOR = 'fore:#666666'
3030
lexers.STYLE_CURSOR = 'back:#585858'
3131
lexers.STYLE_CURSOR_PRIMARY = 'fore:#1c1c1c,back:#87afaf,bold'
32+
lexers.STYLE_CURSOR_MATCHING = 'fore:#1c1c1c,back:#87afaf,bold'
3233
lexers.STYLE_CURSOR_LINE = 'back:#444444'
3334
lexers.STYLE_COLOR_COLUMN = 'back:#444444'
3435
lexers.STYLE_SELECTION = 'back:#5f875f'

lua/vis.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ vis.types.window.set_syntax = function(win, syntax)
259259
win:style_define(win.STYLE_DEFAULT, lexers.STYLE_DEFAULT or '')
260260
win:style_define(win.STYLE_CURSOR, lexers.STYLE_CURSOR or '')
261261
win:style_define(win.STYLE_CURSOR_PRIMARY, lexers.STYLE_CURSOR_PRIMARY or '')
262+
win:style_define(win.STYLE_CURSOR_MATCHING, lexers.STYLE_CURSOR_MATCHING or '')
262263
win:style_define(win.STYLE_CURSOR_LINE, lexers.STYLE_CURSOR_LINE or '')
263264
win:style_define(win.STYLE_SELECTION, lexers.STYLE_SELECTION or '')
264265
win:style_define(win.STYLE_LINENUMBER, lexers.STYLE_LINENUMBER or '')

ui-terminal-curses.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ static void ui_curses_blit(UiTerm *tui) {
237237
cell++;
238238
}
239239
}
240+
move(tui->row, tui->col);
240241
wnoutrefresh(stdscr);
241242
if (tui->doupdate)
242243
doupdate();
@@ -252,7 +253,6 @@ static bool ui_curses_resize(UiTerm *tui, int width, int height) {
252253
}
253254

254255
static void ui_curses_save(UiTerm *tui, bool fscr) {
255-
curs_set(1);
256256
if (fscr) {
257257
def_prog_mode();
258258
endwin();
@@ -264,7 +264,6 @@ static void ui_curses_save(UiTerm *tui, bool fscr) {
264264
static void ui_curses_restore(UiTerm *tui) {
265265
reset_prog_mode();
266266
wclear(stdscr);
267-
curs_set(0);
268267
}
269268

270269
static int ui_curses_colors(Ui *ui) {
@@ -284,7 +283,6 @@ static bool ui_curses_init(UiTerm *tui, char *term) {
284283
nonl();
285284
keypad(stdscr, TRUE);
286285
meta(stdscr, TRUE);
287-
curs_set(0);
288286
return true;
289287
}
290288

ui-terminal-vt100.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*
1313
* - CSI ? 1049 h Save cursor and use Alternate Screen Buffer (DECSET)
1414
* - CSI ? 1049 l Use Normal Screen Buffer and restore cursor (DECRST)
15-
* - CSI ? 25 l Hide Cursor (DECTCEM)
16-
* - CSI ? 25 h Show Cursor (DECTCEM)
1715
* - CSI 2 J Erase in Display (ED)
1816
* - CSI row ; column H Cursor Position (CUP)
1917
* - CSI ... m Character Attributes (SGR)
@@ -104,10 +102,6 @@ static void screen_alternate(bool alternate) {
104102
output_literal(alternate ? "\x1b[?1049h" : "\x1b[0m" "\x1b[?1049l" "\x1b[0m" );
105103
}
106104

107-
static void cursor_visible(bool visible) {
108-
output_literal(visible ? "\x1b[?25h" : "\x1b[?25l");
109-
}
110-
111105
static void ui_vt100_blit(UiTerm *tui) {
112106
Buffer *buf = &((UiVt100*)tui)->buf;
113107
buffer_clear(buf);
@@ -171,6 +165,8 @@ static void ui_vt100_blit(UiTerm *tui) {
171165
cell++;
172166
}
173167
}
168+
/* set terminal's cursor position */
169+
buffer_appendf(buf, "\x1b[%d;%dH", tui->row + 1, tui->col + 1);
174170
output(buffer_content(buf), buffer_length0(buf));
175171
}
176172

@@ -181,11 +177,9 @@ static bool ui_vt100_resize(UiTerm *tui, int width, int height) {
181177
}
182178

183179
static void ui_vt100_save(UiTerm *tui, bool fscr) {
184-
cursor_visible(true);
185180
}
186181

187182
static void ui_vt100_restore(UiTerm *tui) {
188-
cursor_visible(false);
189183
}
190184

191185
static int ui_vt100_colors(Ui *ui) {
@@ -196,13 +190,11 @@ static int ui_vt100_colors(Ui *ui) {
196190
static void ui_vt100_suspend(UiTerm *tui) {
197191
if (!tui->termkey) return;
198192
termkey_stop(tui->termkey);
199-
cursor_visible(true);
200193
screen_alternate(false);
201194
}
202195

203196
static void ui_vt100_resume(UiTerm *tui) {
204197
screen_alternate(true);
205-
cursor_visible(false);
206198
termkey_start(tui->termkey);
207199
}
208200

ui-terminal.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct {
4141
UiTermWin *selwin; /* the currently selected layout */
4242
char info[MAX_WIDTH]; /* info message displayed at the bottom of the screen */
4343
int width, height; /* terminal dimensions available for all windows */
44+
int row, col; /* active cursor's (0-based) position in the terminal */
4445
enum UiLayout layout; /* whether windows are displayed horizontally or vertically */
4546
TermKey *termkey; /* libtermkey instance to handle keyboard input (stdin or /dev/tty) */
4647
size_t ids; /* bit mask of in use window ids */
@@ -372,8 +373,26 @@ static void ui_draw(Ui *ui) {
372373
debug("ui-draw\n");
373374
UiTerm *tui = (UiTerm*)ui;
374375
ui_arrange(ui, tui->layout);
375-
for (UiTermWin *win = tui->windows; win; win = win->next)
376+
int dx = 0, dy = 0;
377+
for (UiTermWin *win = tui->windows; win; win = win->next) {
376378
ui_window_draw((UiWin*)win);
379+
if (win == tui->selwin || win->win->parent) {
380+
View *view = win->win->view;
381+
view_coord_get(view, view_cursor_get(view), NULL, &tui->row, &tui->col);
382+
tui->col += win->sidebar_width;
383+
tui->row += dy;
384+
if (!win->win->parent)
385+
tui->col += dx;
386+
else if (tui->layout == UI_LAYOUT_VERTICAL)
387+
tui->row += win->prev->height;
388+
}
389+
if (tui->layout == UI_LAYOUT_HORIZONTAL)
390+
dy += win->height;
391+
else if (win->win->parent)
392+
dy += win->prev->height;
393+
else
394+
dx += win->width + 1; /* +1 for the |'s */
395+
}
377396
if (tui->info[0])
378397
ui_draw_string(tui, 0, tui->height-1, tui->info, NULL, UI_STYLE_INFO);
379398
ui_term_backend_blit(tui);
@@ -556,6 +575,7 @@ static UiWin *ui_window_new(Ui *ui, Win *w, enum UiOption options) {
556575

557576
styles[UI_STYLE_CURSOR].attr |= CELL_ATTR_REVERSE;
558577
styles[UI_STYLE_CURSOR_PRIMARY].attr |= CELL_ATTR_REVERSE|CELL_ATTR_BLINK;
578+
styles[UI_STYLE_CURSOR_MATCHING].attr |= CELL_ATTR_REVERSE;
559579
styles[UI_STYLE_SELECTION].attr |= CELL_ATTR_REVERSE;
560580
styles[UI_STYLE_COLOR_COLUMN].attr |= CELL_ATTR_REVERSE;
561581
styles[UI_STYLE_STATUS].attr |= CELL_ATTR_REVERSE;

ui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum UiStyle {
3838
UI_STYLE_DEFAULT,
3939
UI_STYLE_CURSOR,
4040
UI_STYLE_CURSOR_PRIMARY,
41+
UI_STYLE_CURSOR_MATCHING,
4142
UI_STYLE_CURSOR_LINE,
4243
UI_STYLE_SELECTION,
4344
UI_STYLE_LINENUMBER,

vis-lua.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,7 @@ void vis_lua_init(Vis *vis) {
32793279
{ UI_STYLE_DEFAULT, "STYLE_DEFAULT" },
32803280
{ UI_STYLE_CURSOR, "STYLE_CURSOR" },
32813281
{ UI_STYLE_CURSOR_PRIMARY, "STYLE_CURSOR_PRIMARY" },
3282+
{ UI_STYLE_CURSOR_MATCHING, "STYLE_CURSOR_MATCHING" },
32823283
{ UI_STYLE_CURSOR_LINE, "STYLE_CURSOR_LINE" },
32833284
{ UI_STYLE_SELECTION, "STYLE_SELECTION" },
32843285
{ UI_STYLE_LINENUMBER, "STYLE_LINENUMBER" },

vis.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,18 @@ static void window_draw_cursor_matching(Win *win, Selection *cur) {
384384
return;
385385
if (!view_coord_get(win->view, pos_match, &line_match, NULL, &col_match))
386386
return;
387-
win->ui->style_set(win->ui, &line_match->cells[col_match], UI_STYLE_SELECTION);
387+
win->ui->style_set(win->ui, &line_match->cells[col_match], UI_STYLE_CURSOR_MATCHING);
388388
}
389389

390-
static void window_draw_cursor(Win *win, Selection *cur) {
390+
static void window_draw_cursor(Win *win, Selection *cur, bool isprimary) {
391391
if (win->vis->win != win)
392392
return;
393393
Line *line = view_cursors_line_get(cur);
394394
int col = view_cursors_cell_get(cur);
395395
if (!line || col == -1)
396396
return;
397-
win->ui->style_set(win->ui, &line->cells[col], UI_STYLE_CURSOR);
397+
win->ui->style_set(win->ui, &line->cells[col],
398+
isprimary ? UI_STYLE_CURSOR_PRIMARY : UI_STYLE_CURSOR);
398399
window_draw_cursor_matching(win, cur);
399400
return;
400401
}
@@ -408,16 +409,16 @@ static void window_draw_selections(Win *win) {
408409
size_t pos = view_cursors_pos(s);
409410
if (pos < viewport.start)
410411
break;
411-
window_draw_cursor(win, s);
412+
window_draw_cursor(win, s, false);
412413
}
413414
window_draw_selection(win, sel);
414-
window_draw_cursor(win, sel);
415+
window_draw_cursor(win, sel, true);
415416
for (Selection *s = view_selections_next(sel); s; s = view_selections_next(s)) {
416417
window_draw_selection(win, s);
417418
size_t pos = view_cursors_pos(s);
418419
if (pos > viewport.end)
419420
break;
420-
window_draw_cursor(win, s);
421+
window_draw_cursor(win, s, false);
421422
}
422423
}
423424

0 commit comments

Comments
 (0)