vim: change cursor shape when only one cursor

In vim mode, change the cursor shape depending on the mode. This is only
applicable if `enable_terminal_cursor` is set to true and there is only
one cursor in the editor.
This commit is contained in:
Tim Culverhouse 2024-08-26 07:02:46 -05:00 committed by CJ van den Berg
parent b115d55097
commit d4b7a6ab9b
6 changed files with 14 additions and 2 deletions

View file

@ -9,6 +9,7 @@ pub const input = @import("input.zig");
pub const Plane = @import("Plane.zig");
pub const Cell = @import("Cell.zig");
pub const CursorShape = vaxis.Cell.CursorShape;
pub const style = @import("style.zig").StyleBits;
@ -355,10 +356,11 @@ pub fn request_mouse_cursor_default(self: *Self, push_or_pop: bool) void {
if (push_or_pop) self.vx.setMouseShape(.default) else self.vx.setMouseShape(.default);
}
pub fn cursor_enable(self: *Self, y: c_int, x: c_int) !void {
pub fn cursor_enable(self: *Self, y: c_int, x: c_int, shape: CursorShape) !void {
self.vx.screen.cursor_vis = true;
self.vx.screen.cursor_row = @intCast(y);
self.vx.screen.cursor_col = @intCast(x);
self.vx.screen.cursor_shape = shape;
}
pub fn cursor_disable(self: *Self) void {

View file

@ -765,7 +765,11 @@ pub const Editor = struct {
fn render_terminal_cursor(self: *const Self, cursor_: *const Cursor) !void {
if (self.screen_cursor(cursor_)) |cursor| {
const y, const x = self.plane.rel_yx_to_abs(@intCast(cursor.row), @intCast(cursor.col));
tui.current().rdr.cursor_enable(y, x) catch {};
const shape = if (tui.current().input_mode) |mode|
mode.cursor_shape
else
.block;
tui.current().rdr.cursor_enable(y, x, shape) catch {};
} else {
tui.current().rdr.cursor_disable();
}
@ -774,6 +778,8 @@ pub const Editor = struct {
fn render_cursors(self: *Self, theme: *const Widget.Theme) !void {
const frame = tracy.initZone(@src(), .{ .name = "editor render cursors" });
defer frame.deinit();
if (self.cursels.items.len == 1 and self.enable_terminal_cursor)
return self.render_terminal_cursor(&self.get_primary().cursor);
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel|
try self.render_cursor(&cursel.cursor, theme);
if (self.enable_terminal_cursor)

View file

@ -35,6 +35,7 @@ pub fn create(a: Allocator) !tui.Mode {
.name = "INSERT",
.description = "vim",
.line_numbers = if (tui.current().config.vim_insert_gutter_line_numbers_relative) .relative else .absolute,
.cursor_shape = .beam,
};
}

View file

@ -37,6 +37,7 @@ pub fn create(a: Allocator) !tui.Mode {
.description = "vim",
.line_numbers = if (tui.current().config.vim_normal_gutter_line_numbers_relative) .relative else .absolute,
.keybind_hints = &hints,
.cursor_shape = .block,
};
}

View file

@ -37,6 +37,7 @@ pub fn create(a: Allocator) !tui.Mode {
.description = "vim",
.line_numbers = if (tui.current().config.vim_visual_gutter_line_numbers_relative) .relative else .absolute,
.keybind_hints = &hints,
.cursor_shape = .underline,
};
}

View file

@ -717,6 +717,7 @@ pub const Mode = struct {
description: []const u8,
line_numbers: enum { absolute, relative } = .absolute,
keybind_hints: ?*const KeybindHints = null,
cursor_shape: renderer.CursorShape = .block,
fn deinit(self: *Mode) void {
self.handler.deinit();