summaryrefslogtreecommitdiff
path: root/lua/clever_f
diff options
context:
space:
mode:
authorJackson Moore <jacksonmoore@tuta.io>2026-09-04 14:27:24 +0200
committerJackson Moore <jacksonmoore@tuta.io>2026-09-04 14:27:24 +0200
commit890cedb3003d45ded2327e483675c42fdfe024f1 (patch)
tree0e7334ba15c6419bf7b7b4fda1ce145c6ee778c6 /lua/clever_f
parentae03a565105416f52a09bc0a171a7fdbb9ced5a0 (diff)
Preserve cursor presentation state
Diffstat (limited to 'lua/clever_f')
-rw-r--r--lua/clever_f/host_adapter.lua72
1 files changed, 72 insertions, 0 deletions
diff --git a/lua/clever_f/host_adapter.lua b/lua/clever_f/host_adapter.lua
index ab3467c..51591a9 100644
--- a/lua/clever_f/host_adapter.lua
+++ b/lua/clever_f/host_adapter.lua
@@ -45,6 +45,7 @@ function HostAdapter.new(options)
next_identity = 1,
highlights = {},
timers = {},
+ cursor_presentations = {},
events = {},
event_order = {},
augroup = nil,
@@ -417,6 +418,77 @@ function HostAdapter:commit_action_transition(token)
return adapter_records[self].event_queue:commit_transition(token)
end
+local function terminal_cursor_option(runtime)
+ return runtime.fn.eval("&t_ve")
+end
+
+local function set_terminal_cursor_option(runtime, value)
+ runtime.api.nvim_cmd({
+ cmd = "let",
+ args = { "&t_ve", "=", runtime.fn.string(value) },
+ }, {})
+end
+
+function HostAdapter:supports_cursor_presentation()
+ local runtime = self:runtime()
+ return type(runtime.api.nvim_get_option_value) == "function"
+ and type(runtime.api.nvim_set_option_value) == "function"
+ and type(runtime.api.nvim_cmd) == "function"
+ and type(runtime.fn) == "table"
+ and type(runtime.fn.exists) == "function"
+ and runtime.fn.exists("+t_ve") == 1
+ and type(runtime.fn.eval) == "function"
+ and type(runtime.fn.string) == "function"
+end
+
+function HostAdapter:suppress_cursor_presentation()
+ if not self:supports_cursor_presentation() then
+ return nil
+ end
+ local record = adapter_records[self]
+ local runtime = record.runtime
+ local identity = next_identity(self, "cursor-presentation")
+ local saved = {
+ guicursor = runtime.api.nvim_get_option_value(
+ "guicursor",
+ { scope = "global" }
+ ),
+ terminal_cursor = terminal_cursor_option(runtime),
+ }
+ runtime.api.nvim_set_option_value(
+ "guicursor",
+ "a:ver1",
+ { scope = "global" }
+ )
+ local ok, failure = pcall(set_terminal_cursor_option, runtime, "")
+ if not ok then
+ runtime.api.nvim_set_option_value(
+ "guicursor",
+ saved.guicursor,
+ { scope = "global" }
+ )
+ error(failure, 0)
+ end
+ record.cursor_presentations[identity] = saved
+ return identity
+end
+
+function HostAdapter:restore_cursor_presentation(identity)
+ local record = adapter_records[self]
+ local saved = record.cursor_presentations[identity]
+ if saved == nil then
+ return false
+ end
+ record.cursor_presentations[identity] = nil
+ record.runtime.api.nvim_set_option_value(
+ "guicursor",
+ saved.guicursor,
+ { scope = "global" }
+ )
+ set_terminal_cursor_option(record.runtime, saved.terminal_cursor)
+ return true
+end
+
local function escape_key(runtime)
if type(runtime.keycode) == "function" then
return runtime.keycode("<Esc>")