From b5c2970fe2324631237171fc853e6156d92b96e3 Mon Sep 17 00:00:00 2001 From: OGAWA Keiji <70509917+kibi2@users.noreply.github.com> Date: Fri, 16 Oct 2026 10:43:15 +0900 Subject: [PATCH 1/3] Add event sequence matching and color scheme demo --- README.md | 2 +- lua/example/demo.lua | 116 +++++++++++ lua/example/keymap.lua | 11 +- lua/example/on_event.lua | 7 +- lua/keyevent/keyevent.lua | 70 ++++--- tests/cases/basic/out-expected.txt | 144 +++++++------- tests/cases/basic/run1.lua | 2 +- tests/cases/diagnosis/out-expected.txt | 24 +-- tests/cases/ng/out-expected.txt | 264 ++++++++++++------------- tests/common_key.lua | 2 +- 10 files changed, 394 insertions(+), 248 deletions(-) create mode 100644 lua/example/demo.lua diff --git a/README.md b/README.md index ef89d0b..86c5d8c 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ local KeyEvent = require("keyevent.keyevent") require("keyevent").on_event(function() if vim.fn.mode() == "i" then - if KeyEvent.keys(2) == "kj" then + if KeyEvent.keys(2, true) == "kj" then local keys = vim.api.nvim_replace_termcodes( "", diff --git a/lua/example/demo.lua b/lua/example/demo.lua new file mode 100644 index 0000000..34b5394 --- /dev/null +++ b/lua/example/demo.lua @@ -0,0 +1,116 @@ +local KeyEvent = require("keyevent.keyevent") +local log = require("keyevent.log") + +local demo_mode = false + +local buf +local win + +local function show() + if not buf or not vim.api.nvim_buf_is_valid(buf) then + return + end + vim.bo[buf].modifiable = true +end + +local function close() + vim.schedule(function() + if win and vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + win = nil + buf = nil + end) +end + +local function create_window() + buf = vim.api.nvim_create_buf(false, true) + vim.bo[buf].buftype = "nofile" + vim.bo[buf].bufhidden = "wipe" + vim.bo[buf].swapfile = false + vim.bo[buf].modifiable = false + local width = 64 + local height = 35 + local ui = vim.api.nvim_list_uis()[1] or { width = 80, height = 24 } + local row = math.floor((ui.height - height) / 2) + local col = math.floor((ui.width - width) / 2) + win = vim.api.nvim_open_win(buf, false, { + relative = "editor", + row = row, + col = col, + width = width, + height = height, + style = "minimal", + border = "rounded", + }) + vim.wo[win].wrap = false + vim.wo[win].cursorline = false + vim.wo[win].number = false + vim.wo[win].relativenumber = false + vim.wo[win].signcolumn = "no" + show() +end + +local origin_scheme +local color_scheme +local ischeme + +local function demo_color_scheme_reset() + if origin_scheme then + vim.cmd.colorscheme(origin_scheme) + end + origin_scheme = nil + color_scheme = nil + ischeme = nil +end + +local function demo_begin() + local seq = KeyEvent.keys(3) + if seq ~= "" then + return + end + demo_mode = true + create_window() +end + +local function demo_end() + local events = KeyEvent.get_events(1) + if events[1].key ~= "" then + return + end + demo_mode = false + demo_color_scheme_reset() + close() +end + +local function demo_color_scheme() + local seq = KeyEvent.keys(1) + if seq ~= "" then + return + end + + if not color_scheme then + origin_scheme = vim.g.colors_name or "default" + color_scheme = vim.fn.getcompletion("", "color") + ischeme = 0 + end + + ischeme = ischeme % #color_scheme + 1 + vim.cmd.colorscheme(color_scheme[ischeme]) + print(color_scheme[ischeme]) +end + +local function on_event() + demo_begin() + demo_end() + if not demo_mode then + return + end + demo_color_scheme() +end + +require("keyevent").on_event(function() + if vim.fn.mode() == "n" then + on_event() + end +end) diff --git a/lua/example/keymap.lua b/lua/example/keymap.lua index acb4926..4583f8c 100644 --- a/lua/example/keymap.lua +++ b/lua/example/keymap.lua @@ -1,10 +1,13 @@ local KeyEvent = require("keyevent.keyevent") +local seqs = { + { key = "n", meta = 0, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, + { key = "n", meta = 0, type = KeyEvent.KEY_EVENT_TYPE.TAP }, + { key = "n", meta = 0, type = KeyEvent.KEY_EVENT_TYPE.REPEAT }, +} vim.keymap.set("n", "n", function() - local event = KeyEvent.keymap_event("n") - if event.nr == 1 and event.nt == 2 then + KeyEvent.keymap_event("n") + if KeyEvent.match(seqs) then return ":history /" - elseif event.nr > 1 and event.nt == 2 then - return "" end return "n" end, { expr = true }) diff --git a/lua/example/on_event.lua b/lua/example/on_event.lua index abc7bb0..7468876 100644 --- a/lua/example/on_event.lua +++ b/lua/example/on_event.lua @@ -1,8 +1,11 @@ local KeyEvent = require("keyevent.keyevent") +local seqs = { + { key = "k", meta = 0 }, + { key = "j", meta = 0, type = KeyEvent.KEY_EVENT_TYPE.TAP }, +} require("keyevent").on_event(function() if vim.fn.mode() == "i" then - local seq = KeyEvent.keys(2) - if seq == "kj" then + if KeyEvent.match(seqs) then local keys = vim.api.nvim_replace_termcodes( "", true, diff --git a/lua/keyevent/keyevent.lua b/lua/keyevent/keyevent.lua index 49e27b1..1f066a2 100644 --- a/lua/keyevent/keyevent.lua +++ b/lua/keyevent/keyevent.lua @@ -46,6 +46,11 @@ local STATE = { local callbacks = {} +---@class KeySeq +---@field key string +---@field meta integer +---@field type KeyEventType + ---@class KeyEvent ---@field source KeyEventSource ---@field bufnr integer @@ -313,22 +318,6 @@ local function on_key(_, typed) on_key_event(vim.fn.keytrans(typed)) end ----@param key string ----@param meta integer ----@return string -local function key_note(key, meta) - local note = M.unparse(key, meta) - if not note then - if meta == 0 then - return string.format("%s", key) - else - return string.format("%d-%s", meta, key) - end - else - return note:match("^<(.+)>$") or note - end -end - function M.meta() return META end @@ -414,6 +403,17 @@ function M.unparse(key, meta) return "<" .. table.concat(chars, "-") .. ">" end +---@param key string +---@param meta integer +---@return string +local function meta_key(key, meta) + if meta == 0 then + return key + else + return string.format("%d-%s", meta, key) + end +end + ---@param event KeyEvent ---@return string function M.to_string(event) @@ -424,16 +424,18 @@ function M.to_string(event) ng_repeat = event.ng_repeat .. "-" end return string.format( - "%s %-6s (%d %d %2d) %3s (%3d %d) %s", + "%s %-6s %3s (%d %d %2d) (%3d %d) %s", + -- "%s %-6s (%d %d %2d) %3s (%3d %d) %s", event.source, ng_repeat .. event.type, + meta_key(event.key, event.meta), event.nt, event.nh, event.nr, - key_note(event.key, event.meta), + -- key_note(event.key, event.meta), event.interval, math.floor(M.hold_time(event) / 1000), - M.keys(10) + M.keys(10, true) ) end @@ -481,7 +483,7 @@ end ---@param count integer ---@return KeyEvent[] -function M.get_evcents(count) +function M.get_events(count) ---@type KeyEvent[] local seq = {} for index = 1, count do @@ -491,10 +493,12 @@ function M.get_evcents(count) end ---@param count integer +---@param break_click boolean|nil ---@return string -function M.keys(count) +function M.keys(count, break_click) + break_click = break_click == true local seq = {} - for _, event in ipairs(M.get_evcents(count)) do + for _, event in ipairs(M.get_events(count)) do if event.key then local note = M.unparse(event.key, event.meta) or "" note = note:match("^<(.)>$") or note @@ -502,7 +506,7 @@ function M.keys(count) note = string.format("↻") end seq[#seq + 1] = note - if event.type == M.KEY_EVENT_TYPE.CLICK then + if break_click and event.type == M.KEY_EVENT_TYPE.CLICK then break end end @@ -514,6 +518,26 @@ function M.keys(count) return table.concat(reversed) end +---@param seqs KeySeq[] +---@return boolean +function M.match(seqs) + local events = M.get_events(#seqs) + for ievent = 1, #seqs do + local event = events[ievent] or {} + local seq = seqs[#seqs + 1 - ievent] + if seq.key and seq.key ~= event.key then + return false + end + if seq.meta and seq.meta ~= event.meta then + return false + end + if seq.type and seq.type ~= event.type then + return false + end + end + return true +end + ---@param index integer ---@return KeyEvent function M.peek(index) diff --git a/tests/cases/basic/out-expected.txt b/tests/cases/basic/out-expected.txt index f5c7f96..5a03c49 100644 --- a/tests/cases/basic/out-expected.txt +++ b/tests/cases/basic/out-expected.txt @@ -2,85 +2,85 @@ === MESSAGE === --- CASE 6: : tan tan ta ta tan --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk map repeat (1 1 1) j (500 0) j↻ -MAP map repeat (1 1 1) j (500 0) j↻ -onk onk repeat (1 1 2) j (150 0) j↻ -onk onk repeat (1 1 3) j (150 0) j↻ -onk onk repeat (1 1 4) j (150 0) j↻ -onk onk tap (1 1 0) j (300 0) j↻j -onk onk repeat (1 2 1) j (500 0) j↻j↻ -onk onk repeat (1 2 2) j (150 0) j↻j↻ -onk onk tap (1 2 0) j (300 0) j↻j↻j -onk onk tap (2 2 0) j (300 0) j↻j↻jj -onk onk tap (3 2 0) j (300 0) j↻j↻jjj -onk onk repeat (3 3 1) j (500 0) j↻j↻jjj↻ -onk onk repeat (3 3 2) j (150 0) j↻j↻jjj↻ -onk onk repeat (3 3 3) j (150 0) j↻j↻jjj↻ -onk onk repeat (3 3 4) j (150 0) j↻j↻jjj↻ -onk onk repeat (3 3 5) j (150 1) j↻j↻jjj↻ -onk onk repeat (3 3 6) j (150 1) j↻j↻jjj↻ -onk onk repeat (3 3 7) j (150 1) j↻j↻jjj↻ +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk map repeat j (1 1 1) (500 0) j↻ +MAP map repeat j (1 1 1) (500 0) j↻ +onk onk repeat j (1 1 2) (150 0) j↻ +onk onk repeat j (1 1 3) (150 0) j↻ +onk onk repeat j (1 1 4) (150 0) j↻ +onk onk tap j (1 1 0) (300 0) j↻j +onk onk repeat j (1 2 1) (500 0) j↻j↻ +onk onk repeat j (1 2 2) (150 0) j↻j↻ +onk onk tap j (1 2 0) (300 0) j↻j↻j +onk onk tap j (2 2 0) (300 0) j↻j↻jj +onk onk tap j (3 2 0) (300 0) j↻j↻jjj +onk onk repeat j (3 3 1) (500 0) j↻j↻jjj↻ +onk onk repeat j (3 3 2) (150 0) j↻j↻jjj↻ +onk onk repeat j (3 3 3) (150 0) j↻j↻jjj↻ +onk onk repeat j (3 3 4) (150 0) j↻j↻jjj↻ +onk onk repeat j (3 3 5) (150 1) j↻j↻jjj↻ +onk onk repeat j (3 3 6) (150 1) j↻j↻jjj↻ +onk onk repeat j (3 3 7) (150 1) j↻j↻jjj↻ hold_time = 500 + 150 * 6 = 1400 --- CASE 9: : tan tan ta ta tan (meta) --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk map repeat (1 1 1) M-j (520 0) j↻ -MAP map repeat (1 1 1) M-j (520 0) j↻ -onk onk repeat (1 1 2) S-j (170 0) j↻ -onk onk repeat (1 1 3) C-j (130 0) j↻ -onk onk repeat (1 1 4) A-j (150 0) j↻ -onk onk tap (1 1 0) D-j (280 0) j↻ -onk onk repeat (1 2 1) D-j (500 0) j↻↻ -onk onk repeat (1 2 2) T-j (150 0) j↻↻ -onk onk tap (1 2 0) T-j (280 0) j↻↻ -onk onk tap (2 2 0) j (320 0) j↻↻j -onk onk tap (3 2 0) S-j (300 0) j↻↻j -onk onk repeat (3 3 1) T-j (480 0) j↻↻j↻ -onk onk repeat (3 3 2) j (150 0) j↻↻j↻ -onk onk click (1 0 0) k (950 0) k +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk map repeat 8-j (1 1 1) (520 0) j↻ +MAP map repeat 8-j (1 1 1) (520 0) j↻ +onk onk repeat 1-j (1 1 2) (170 0) j↻ +onk onk repeat 2-j (1 1 3) (130 0) j↻ +onk onk repeat 4-j (1 1 4) (150 0) j↻ +onk onk tap 16-j (1 1 0) (280 0) j↻ +onk onk repeat 16-j (1 2 1) (500 0) j↻↻ +onk onk repeat 32-j (1 2 2) (150 0) j↻↻ +onk onk tap 32-j (1 2 0) (280 0) j↻↻ +onk onk tap j (2 2 0) (320 0) j↻↻j +onk onk tap 1-j (3 2 0) (300 0) j↻↻j +onk onk repeat 32-j (3 3 1) (480 0) j↻↻j↻ +onk onk repeat j (3 3 2) (150 0) j↻↻j↻ +onk onk click k (1 0 0) (950 0) k --- CASE 12: : tan tan ta ta tan (diff key) --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) x (900 0) x -onk map repeat (1 1 1) x (500 0) x↻ -MAP map repeat (1 1 1) x (500 0) x↻ -onk onk repeat (1 1 2) x (150 0) x↻ -onk onk repeat (1 1 3) x (150 0) x↻ -onk onk repeat (1 1 4) x (150 0) x↻ -onk onk tap (1 1 0) h (300 0) x↻h -onk onk repeat (1 2 1) h (500 0) x↻h↻ -onk onk repeat (1 2 2) h (150 0) x↻h↻ -onk onk tap (1 2 0) w (300 0) x↻h↻w -onk onk tap (1 2 0) e (300 0) x↻h↻we -onk onk tap (1 2 0) j (300 0) x↻h↻wej -onk onk repeat (1 3 1) j (500 0) x↻h↻wej↻ -onk onk repeat (1 3 2) j (150 0) x↻h↻wej↻ -onk onk click (1 0 0) k (900 0) k +PLC src type key (t h r) (itv h) seq +onk onk click x (1 0 0) (900 0) x +onk map repeat x (1 1 1) (500 0) x↻ +MAP map repeat x (1 1 1) (500 0) x↻ +onk onk repeat x (1 1 2) (150 0) x↻ +onk onk repeat x (1 1 3) (150 0) x↻ +onk onk repeat x (1 1 4) (150 0) x↻ +onk onk tap h (1 1 0) (300 0) x↻h +onk onk repeat h (1 2 1) (500 0) x↻h↻ +onk onk repeat h (1 2 2) (150 0) x↻h↻ +onk onk tap w (1 2 0) (300 0) x↻h↻w +onk onk tap e (1 2 0) (300 0) x↻h↻we +onk onk tap j (1 2 0) (300 0) x↻h↻wej +onk onk repeat j (1 3 1) (500 0) x↻h↻wej↻ +onk onk repeat j (1 3 2) (150 0) x↻h↻wej↻ +onk onk click k (1 0 0) (900 0) k --- CASE 15: : tan tan ta ta tan (interval) --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk repeat (1 1 1) j (200 0) j↻ -onk onk repeat (1 1 2) j (300 0) j↻ -onk onk repeat (1 1 3) j (300 0) j↻ -onk onk repeat (1 1 4) j (300 1) j↻ -onk onk tap (1 1 0) j (100 0) j↻j -onk onk repeat (1 2 1) j (200 0) j↻j↻ -onk onk repeat (1 2 2) j (300 0) j↻j↻ -onk onk tap (1 2 0) j (100 0) j↻j↻j -onk onk tap (2 2 0) j (100 0) j↻j↻jj -onk onk tap (3 2 0) j (100 0) j↻j↻jjj -onk onk repeat (3 3 1) j (200 0) j↻j↻jjj↻ -onk onk repeat (3 3 2) j (300 0) j↻j↻jjj↻ -onk onk repeat (3 3 3) j (300 0) j↻j↻jjj↻ -onk onk repeat (3 3 4) j (300 1) j↻j↻jjj↻ -onk onk repeat (3 3 5) j (300 1) j↻j↻jjj↻ -onk onk repeat (3 3 6) j (300 1) j↻j↻jjj↻ -onk onk repeat (3 3 7) j (300 2) j↻j↻jjj↻ -onk onk click (1 0 0) j (311 0) j +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk repeat j (1 1 1) (200 0) j↻ +onk onk repeat j (1 1 2) (300 0) j↻ +onk onk repeat j (1 1 3) (300 0) j↻ +onk onk repeat j (1 1 4) (300 1) j↻ +onk onk tap j (1 1 0) (100 0) j↻j +onk onk repeat j (1 2 1) (200 0) j↻j↻ +onk onk repeat j (1 2 2) (300 0) j↻j↻ +onk onk tap j (1 2 0) (100 0) j↻j↻j +onk onk tap j (2 2 0) (100 0) j↻j↻jj +onk onk tap j (3 2 0) (100 0) j↻j↻jjj +onk onk repeat j (3 3 1) (200 0) j↻j↻jjj↻ +onk onk repeat j (3 3 2) (300 0) j↻j↻jjj↻ +onk onk repeat j (3 3 3) (300 0) j↻j↻jjj↻ +onk onk repeat j (3 3 4) (300 1) j↻j↻jjj↻ +onk onk repeat j (3 3 5) (300 1) j↻j↻jjj↻ +onk onk repeat j (3 3 6) (300 1) j↻j↻jjj↻ +onk onk repeat j (3 3 7) (300 2) j↻j↻jjj↻ +onk onk click j (1 0 0) (311 0) j === DISPLAY === diff --git a/tests/cases/basic/run1.lua b/tests/cases/basic/run1.lua index 25abf99..004ba3a 100644 --- a/tests/cases/basic/run1.lua +++ b/tests/cases/basic/run1.lua @@ -23,5 +23,5 @@ local tan_tan_ta_ta_tann = { } common_key.run_sequence(tan_tan_ta_ta_tann) -local events = KeyEvent.get_evcents(2) +local events = KeyEvent.get_events(2) print("hold_time = 500 + 150 * 6 = ", KeyEvent.hold_time(events[1])) diff --git a/tests/cases/diagnosis/out-expected.txt b/tests/cases/diagnosis/out-expected.txt index c03eecd..6dbfbc3 100644 --- a/tests/cases/diagnosis/out-expected.txt +++ b/tests/cases/diagnosis/out-expected.txt @@ -2,18 +2,18 @@ === MESSAGE === --- CASE 6: diagnosis --- -onk onk click (1 0 0) j (900 0) j -onk onk repeat (1 1 1) j (500 0) j↻ -onk onk repeat (1 1 2) j (150 0) j↻ -onk onk repeat (1 1 3) j (150 0) j↻ -onk onk click (1 0 0) h (900 0) h -onk onk repeat (1 1 1) h (500 0) h↻ -onk onk repeat (1 1 2) h (150 0) h↻ -onk onk tap (1 1 0) k (300 0) h↻k -onk onk repeat (1 2 1) k (500 0) h↻k↻ -onk onk repeat (1 2 2) k (150 0) h↻k↻ -onk onk repeat (1 2 3) k (150 0) h↻k↻ -onk onk tap (1 2 0) (150 0) h↻k↻ +onk onk click j (1 0 0) (900 0) j +onk onk repeat j (1 1 1) (500 0) j↻ +onk onk repeat j (1 1 2) (150 0) j↻ +onk onk repeat j (1 1 3) (150 0) j↻ +onk onk click h (1 0 0) (900 0) h +onk onk repeat h (1 1 1) (500 0) h↻ +onk onk repeat h (1 1 2) (150 0) h↻ +onk onk tap k (1 1 0) (300 0) h↻k +onk onk repeat k (1 2 1) (500 0) h↻k↻ +onk onk repeat k (1 2 2) (150 0) h↻k↻ +onk onk repeat k (1 2 3) (150 0) h↻k↻ +onk onk tap (1 2 0) (150 0) h↻k↻ 500 150 --- CASE 38: NG command --- diff --git a/tests/cases/ng/out-expected.txt b/tests/cases/ng/out-expected.txt index 87a003f..9824d73 100644 --- a/tests/cases/ng/out-expected.txt +++ b/tests/cases/ng/out-expected.txt @@ -4,154 +4,154 @@ --- CASE 6: : tan click NG --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ -onk onk repeat (2 1 4) j (150 0) jj↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk click (1 0 0) j (900 0) j -onk onk repeat (1 1 1) j (500 0) j↻ -onk onk repeat (1 1 2) j (150 0) j↻ -onk onk repeat (1 1 3) j (150 0) j↻ -onk onk repeat (1 1 4) j (150 0) j↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk click (1 0 0) j (900 0) j -onk onk 1-tap (2 0 0) j (150 0) jj -onk onk 2-tap (3 0 0) j (150 0) jjj -onk onk 3-tap (4 0 0) j (150 0) jjjj - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk click (1 0 0) j (900 0) j -onk onk 1-tap (2 0 0) j (150 0) jj -onk onk 2-tap (3 0 0) j (150 0) jjj - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk click (1 0 0) j (900 0) j -onk onk 1-tap (2 0 0) j (150 0) jj +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ +onk onk repeat j (2 1 4) (150 0) jj↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk click j (1 0 0) (900 0) j +onk onk repeat j (1 1 1) (500 0) j↻ +onk onk repeat j (1 1 2) (150 0) j↻ +onk onk repeat j (1 1 3) (150 0) j↻ +onk onk repeat j (1 1 4) (150 0) j↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk click j (1 0 0) (900 0) j +onk onk 1-tap j (2 0 0) (150 0) jj +onk onk 2-tap j (3 0 0) (150 0) jjj +onk onk 3-tap j (4 0 0) (150 0) jjjj + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk click j (1 0 0) (900 0) j +onk onk 1-tap j (2 0 0) (150 0) jj +onk onk 2-tap j (3 0 0) (150 0) jjj + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk click j (1 0 0) (900 0) j +onk onk 1-tap j (2 0 0) (150 0) jj --- CASE 9: : tan hold NG --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ -onk onk repeat (2 1 4) j (150 0) jj↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk repeat (1 1 1) j (500 0) j↻ -onk onk 1-repeat (1 1 1) j (500 0) j↻ -onk onk repeat (1 1 2) j (150 0) j↻ -onk onk repeat (1 1 3) j (150 0) j↻ -onk onk repeat (1 1 4) j (150 0) j↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk 1-repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk 1-tap (1 1 0) j (500 0) jj↻j -onk onk 2-tap (2 1 0) j (150 0) jj↻jj +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ +onk onk repeat j (2 1 4) (150 0) jj↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk repeat j (1 1 1) (500 0) j↻ +onk onk 1-repeat j (1 1 1) (500 0) j↻ +onk onk repeat j (1 1 2) (150 0) j↻ +onk onk repeat j (1 1 3) (150 0) j↻ +onk onk repeat j (1 1 4) (150 0) j↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk 1-repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk 1-tap j (1 1 0) (500 0) jj↻j +onk onk 2-tap j (2 1 0) (150 0) jj↻jj --- CASE 12: : tan repeat NG --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ -onk onk repeat (2 1 4) j (150 0) jj↻ +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ +onk onk repeat j (2 1 4) (150 0) jj↻ -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk 1-tap (2 0 0) j (150 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ -onk onk repeat (2 1 4) j (150 0) jj↻ +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk 1-tap j (2 0 0) (150 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ +onk onk repeat j (2 1 4) (150 0) jj↻ -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk 1-tap (3 0 0) j (150 0) jjj -onk onk 2-tap (4 0 0) j (150 0) jjjj -onk onk 3-tap (5 0 0) j (150 0) jjjjj -onk onk 4-tap (6 0 0) j (150 0) jjjjjj +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk 1-tap j (3 0 0) (150 0) jjj +onk onk 2-tap j (4 0 0) (150 0) jjjj +onk onk 3-tap j (5 0 0) (150 0) jjjjj +onk onk 4-tap j (6 0 0) (150 0) jjjjjj --- CASE 15: : tan tap NG --- -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk repeat (2 1 3) j (150 0) jj↻ -onk onk repeat (2 1 4) j (150 0) jj↻ - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk tap (3 0 0) j (300 0) jjj -onk onk 1-tap (4 0 0) j (150 0) jjjj -onk onk 2-tap (5 0 0) j (150 0) jjjjj -onk onk 3-tap (6 0 0) j (150 0) jjjjjj - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk tap (1 1 0) j (300 0) jj↻j -onk onk 1-tap (2 1 0) j (150 0) jj↻jj -onk onk 2-tap (3 1 0) j (150 0) jj↻jjj - - -PLC src type (t h r) key (itv h) seq -onk onk click (1 0 0) j (900 0) j -onk onk tap (2 0 0) j (300 0) jj -onk onk repeat (2 1 1) j (500 0) jj↻ -onk onk repeat (2 1 2) j (150 0) jj↻ -onk onk tap (1 1 0) j (300 0) jj↻j -onk onk 1-tap (2 1 0) j (150 0) jj↻jj +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk repeat j (2 1 3) (150 0) jj↻ +onk onk repeat j (2 1 4) (150 0) jj↻ + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk tap j (3 0 0) (300 0) jjj +onk onk 1-tap j (4 0 0) (150 0) jjjj +onk onk 2-tap j (5 0 0) (150 0) jjjjj +onk onk 3-tap j (6 0 0) (150 0) jjjjjj + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk tap j (1 1 0) (300 0) jj↻j +onk onk 1-tap j (2 1 0) (150 0) jj↻jj +onk onk 2-tap j (3 1 0) (150 0) jj↻jjj + + +PLC src type key (t h r) (itv h) seq +onk onk click j (1 0 0) (900 0) j +onk onk tap j (2 0 0) (300 0) jj +onk onk repeat j (2 1 1) (500 0) jj↻ +onk onk repeat j (2 1 2) (150 0) jj↻ +onk onk tap j (1 1 0) (300 0) jj↻j +onk onk 1-tap j (2 1 0) (150 0) jj↻jj === DISPLAY === diff --git a/tests/common_key.lua b/tests/common_key.lua index a6d6055..bbc0f70 100644 --- a/tests/common_key.lua +++ b/tests/common_key.lua @@ -21,7 +21,7 @@ end --- @param sequence KeyInfo[] function M.run_sequence(sequence) - print("PLC src type (t h r) key (itv h) seq") + print("PLC src type key (t h r) (itv h) seq") for _, keyinfo in ipairs(sequence) do M.advance(keyinfo.interval) if keyinfo.keymap then From 75e64bb7e1e5223c5bf9d358380e04ffd195ab2e Mon Sep 17 00:00:00 2001 From: OGAWA Keiji <70509917+kibi2@users.noreply.github.com> Date: Mon, 19 Oct 2026 13:54:06 +0900 Subject: [PATCH 2/3] =?UTF-8?q?WIP:=20=E3=83=87=E3=83=A2=E5=8B=95=E3=81=84?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 + lua/example/demo.lua | 116 -------- lua/example/demo_scheme.lua | 278 ++++++++++++++++++ lua/keyevent/keyevent.lua | 2 +- tests/cases/example/on_event/out-expected.txt | 27 ++ tests/cases/example/on_event/run.lua | 31 ++ tests/cases/example/on_event/run.vim | 9 + tests/cases/example/on_event/skip-ci | 0 8 files changed, 353 insertions(+), 117 deletions(-) delete mode 100644 lua/example/demo.lua create mode 100644 lua/example/demo_scheme.lua create mode 100644 tests/cases/example/on_event/out-expected.txt create mode 100644 tests/cases/example/on_event/run.lua create mode 100644 tests/cases/example/on_event/run.vim create mode 100644 tests/cases/example/on_event/skip-ci diff --git a/CHANGELOG.md b/CHANGELOG.md index 115f9cc..1a7aeca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,14 @@ # Changelog +All notable changes to this project will be documented in this file. + ## [0.1.1] - TBD +### Added + +- Add a color scheme demo to demonstrate key event recognition and slideshow control. +- Add `KeyEvent.match()` to match recent events against a sequence of key event specifications. + ### Changed - Move logging to optional `logger.nvim`; logging is disabled by default. diff --git a/lua/example/demo.lua b/lua/example/demo.lua deleted file mode 100644 index 34b5394..0000000 --- a/lua/example/demo.lua +++ /dev/null @@ -1,116 +0,0 @@ -local KeyEvent = require("keyevent.keyevent") -local log = require("keyevent.log") - -local demo_mode = false - -local buf -local win - -local function show() - if not buf or not vim.api.nvim_buf_is_valid(buf) then - return - end - vim.bo[buf].modifiable = true -end - -local function close() - vim.schedule(function() - if win and vim.api.nvim_win_is_valid(win) then - vim.api.nvim_win_close(win, true) - end - win = nil - buf = nil - end) -end - -local function create_window() - buf = vim.api.nvim_create_buf(false, true) - vim.bo[buf].buftype = "nofile" - vim.bo[buf].bufhidden = "wipe" - vim.bo[buf].swapfile = false - vim.bo[buf].modifiable = false - local width = 64 - local height = 35 - local ui = vim.api.nvim_list_uis()[1] or { width = 80, height = 24 } - local row = math.floor((ui.height - height) / 2) - local col = math.floor((ui.width - width) / 2) - win = vim.api.nvim_open_win(buf, false, { - relative = "editor", - row = row, - col = col, - width = width, - height = height, - style = "minimal", - border = "rounded", - }) - vim.wo[win].wrap = false - vim.wo[win].cursorline = false - vim.wo[win].number = false - vim.wo[win].relativenumber = false - vim.wo[win].signcolumn = "no" - show() -end - -local origin_scheme -local color_scheme -local ischeme - -local function demo_color_scheme_reset() - if origin_scheme then - vim.cmd.colorscheme(origin_scheme) - end - origin_scheme = nil - color_scheme = nil - ischeme = nil -end - -local function demo_begin() - local seq = KeyEvent.keys(3) - if seq ~= "" then - return - end - demo_mode = true - create_window() -end - -local function demo_end() - local events = KeyEvent.get_events(1) - if events[1].key ~= "" then - return - end - demo_mode = false - demo_color_scheme_reset() - close() -end - -local function demo_color_scheme() - local seq = KeyEvent.keys(1) - if seq ~= "" then - return - end - - if not color_scheme then - origin_scheme = vim.g.colors_name or "default" - color_scheme = vim.fn.getcompletion("", "color") - ischeme = 0 - end - - ischeme = ischeme % #color_scheme + 1 - vim.cmd.colorscheme(color_scheme[ischeme]) - print(color_scheme[ischeme]) -end - -local function on_event() - demo_begin() - demo_end() - if not demo_mode then - return - end - demo_color_scheme() -end - -require("keyevent").on_event(function() - if vim.fn.mode() == "n" then - on_event() - end -end) diff --git a/lua/example/demo_scheme.lua b/lua/example/demo_scheme.lua new file mode 100644 index 0000000..5c19b15 --- /dev/null +++ b/lua/example/demo_scheme.lua @@ -0,0 +1,278 @@ +local KeyEvent = require("keyevent.keyevent") +local log = require("keyevent.log") +---@enum DemoScheme +local STATE = { + NORMAL = "NORMAL", + DEMO = "DEMO", + HOLD = "HOLD", + HOLD_R = "HOLD_R", + SLIDE = "SLIDE", +} + +---@type DemoScheme +local state = STATE.NORMAL +local META = KeyEvent.meta() +local repeat_timer = vim.loop.new_timer() +local scheme = nil + +local buf +local win + +local function color_name() + if not scheme then + return vim.g.colors_name or "default" + end + return scheme.colors[scheme.index] +end + +local function show(interval) + if not buf or not vim.api.nvim_buf_is_valid(buf) then + return + end + local lines = { + "===== keyevent.nvim: Color Scheme Demo =====", + "", + "HH + HH (click H, tap H, click H, tap H): Open this window", + "Click H: Change color scheme", + "Click h: Change color scheme in reverse order", + "Hold H: Start slideshow on release", + " Hold duration determines slideshow interval", + "Tap H + hold H: Start slideshow in reverse order", + "Any key: Stop slideshow", + "Esc: Exit demo", + "--------------------------------------------", + "", + "Color name: " .. color_name(), + "Slideshow interval: " .. (interval and (interval .. " ms") or "N/A"), + } + vim.schedule(function() + vim.bo[buf].modifiable = true + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) + vim.bo[buf].modifiable = false + end) +end + +local function close() + vim.schedule(function() + if win and vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + win = nil + buf = nil + end) +end + +local function create_window() + buf = vim.api.nvim_create_buf(false, true) + vim.bo[buf].buftype = "nofile" + vim.bo[buf].bufhidden = "wipe" + vim.bo[buf].swapfile = false + vim.bo[buf].modifiable = false + local width = 64 + local height = 15 + local ui = vim.api.nvim_list_uis()[1] or { width = 80, height = 24 } + local row = math.floor((ui.height - height) / 2) + local col = math.floor((ui.width - width) / 2) + win = vim.api.nvim_open_win(buf, false, { + relative = "editor", + row = row, + col = col, + width = width, + height = height, + style = "minimal", + border = "rounded", + }) + vim.wo[win].wrap = false + vim.wo[win].cursorline = false + vim.wo[win].number = false + vim.wo[win].relativenumber = false + vim.wo[win].signcolumn = "no" + show() +end + +local seqs_esc = { + { key = "" }, +} +local seqs_tatan_tatan = { + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.TAP }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.TAP }, +} +local seqs_tan = { + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, +} +local seqs_tan_h = { + { key = "h", meta = 0, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, +} +local seqs_taan = { + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.REPEAT }, +} +local seqs_tataan = { + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.CLICK }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.TAP }, + { key = "h", meta = META.S, type = KeyEvent.KEY_EVENT_TYPE.REPEAT }, +} + +local function demo_color_scheme_set() + local origin = vim.g.colors_name or "default" + local colors = vim.fn.getcompletion("", "color") + local index = vim.fn.index(colors, origin) + scheme = { + origin = origin, + colors = colors, + index = index >= 0 and index + 1 or 1, + } +end + +local function demo_color_scheme_reset() + if scheme then + vim.cmd.colorscheme(scheme.origin) + end + scheme = nil +end + +---@param index integer +---@param max integer +---@param reverse boolean +---@return integer +local function next_index(index, max, reverse) + local step = reverse and -1 or 1 + index = index + step + index = index % max + if index == 0 then + index = max + end + return index +end + +---@param reverse boolean +---@param interval integer|nil +local function demo_color_scheme(reverse, interval) + if not scheme then + return + end + scheme.index = next_index(scheme.index, #scheme.colors, reverse) + local name = scheme.colors[scheme.index] + vim.cmd.colorscheme(name) + log.debug(name) + show(interval) +end + +local function stop_repeat_timer() + repeat_timer:stop() +end + +local function close_repeat_timer() + if not repeat_timer:is_closing() then + repeat_timer:stop() + repeat_timer:close() + end +end + +---@param interval integer +---@param reverse boolean +local function start_repeat_timer(interval, reverse) + log.debug(interval, reverse) + repeat_timer:start( + 0, + interval, + vim.schedule_wrap(function() + demo_color_scheme(reverse, interval) + end) + ) +end + +local function demo_start() + demo_color_scheme_set() + create_window() +end + +local function demo_end() + demo_color_scheme_reset() + close() +end + +local function normal() + if not KeyEvent.match(seqs_tatan_tatan) then + return + end + demo_start() + state = STATE.DEMO + log.debug("demo start") +end + +local function demo() + if KeyEvent.match(seqs_esc) then + demo_end() + state = STATE.NORMAL + log.debug("demo end") + return + end + if KeyEvent.match(seqs_tan) then + demo_color_scheme(false) + elseif KeyEvent.match(seqs_tan_h) then + demo_color_scheme(true) + end + if KeyEvent.match(seqs_tataan) then + state = STATE.HOLD_R + elseif KeyEvent.match(seqs_taan) then + state = STATE.HOLD + end +end + +---@param event KeyEvent +---@param reverse boolean +local function hold(event, reverse) + local interval = KeyEvent.hold_time(event) + if event.type == KeyEvent.KEY_EVENT_TYPE.REPEAT_END then + start_repeat_timer(interval, reverse) + state = STATE.SLIDE + log.debug(string.format("slide start : %d %s ", interval, reverse)) + elseif event.type == KeyEvent.KEY_EVENT_TYPE.REPEAT then + show(interval) + else + state = STATE.DEMO + show(0) + log.debug("hold end") + end +end + +local function slide() + stop_repeat_timer() + if KeyEvent.match(seqs_esc) then + demo_end() + state = STATE.NORMAL + log.debug("demo end") + else + state = STATE.DEMO + log.debug("slide end") + end +end + +local function on_event(event) + if state == STATE.NORMAL then + normal() + elseif state == STATE.DEMO then + demo() + elseif state == STATE.HOLD then + hold(event, false) + elseif state == STATE.HOLD_R then + hold(event, true) + elseif state == STATE.SLIDE then + slide() + else + assert(false, state) + end +end + +require("keyevent").on_event(function(event) + if vim.fn.mode() == "n" then + on_event(event) + end +end) + +vim.api.nvim_create_autocmd("VimLeavePre", { + callback = close_repeat_timer, +}) diff --git a/lua/keyevent/keyevent.lua b/lua/keyevent/keyevent.lua index 1f066a2..9bc7a07 100644 --- a/lua/keyevent/keyevent.lua +++ b/lua/keyevent/keyevent.lua @@ -471,7 +471,7 @@ end ---@param event KeyEvent ---@return integer function M.hold_time(event) - if event.type ~= M.KEY_EVENT_TYPE.REPEAT then + if event.hold_start == 0 then return 0 end return event.time - event.hold_start diff --git a/tests/cases/example/on_event/out-expected.txt b/tests/cases/example/on_event/out-expected.txt new file mode 100644 index 0000000..cc60e05 --- /dev/null +++ b/tests/cases/example/on_event/out-expected.txt @@ -0,0 +1,27 @@ +--- example demo --- +=== MESSAGE === + +--- CASE 6: : example demo --- +[DEBUG][run.lua] hoge +PLC src type key (t h r) (itv h) seq +onk onk click 1-h (1 0 0) (900 0) +onk onk tap 1-h (2 0 0) (300 0) +onk onk click 1-h (1 0 0) (900 0) +onk onk tap 1-h (2 0 0) (300 0) +[DEBUG][demo_scheme.lua] demo start +onk onk click 1-h (1 0 0) (900 0) +[DEBUG][demo_scheme.lua] delek +onk onk click 1-h (1 0 0) (900 0) +[DEBUG][demo_scheme.lua] desert +onk onk 1-tap 1-h (2 0 0) (150 0) +onk onk click 1-l (1 0 0) (900 0) +onk onk click 1-h (1 0 0) (900 0) +[DEBUG][demo_scheme.lua] elflord +onk onk tap 1-h (2 0 0) (300 0) +onk onk 1-tap 1-h (3 0 0) (150 0) +onk onk click 1-l (1 0 0) (900 0) +onk onk tap (1 0 0) (300 0) +[DEBUG][demo_scheme.lua] demo end + +=== DISPLAY === + diff --git a/tests/cases/example/on_event/run.lua b/tests/cases/example/on_event/run.lua new file mode 100644 index 0000000..67a0067 --- /dev/null +++ b/tests/cases/example/on_event/run.lua @@ -0,0 +1,31 @@ +local common_key = require("common_key") +local root = assert(os.getenv("KIBI2_REPO_ROOT")) +vim.opt.rtp:prepend(root) +vim.opt.rtp:prepend(root .. "/../logger.nvim") +package.loaded["keyevent.log"] = nil +local log = require("keyevent.log") +require("example.demo_scheme") +log.setup({ + level = vim.log.levels.DEBUG, + output = "print", +}) +log.debug("hoge") + +---@type KeyInfo[] +local demo = { + { key = "H", interval = CLICK }, + { key = "H", interval = TAP }, + { key = "H", interval = CLICK }, + { key = "H", interval = TAP }, + { key = "H", interval = CLICK }, -- next + { key = "H", interval = CLICK }, -- slide + { key = "H", interval = REPEAT }, + { key = "L", interval = CLICK }, -- end + { key = "H", interval = CLICK }, -- slide reverse + { key = "H", interval = TAP }, + { key = "H", interval = REPEAT }, + { key = "L", interval = CLICK }, -- end + { key = "", interval = TAP }, +} + +common_key.run_sequence(demo) diff --git a/tests/cases/example/on_event/run.vim b/tests/cases/example/on_event/run.vim new file mode 100644 index 0000000..178a0c5 --- /dev/null +++ b/tests/cases/example/on_event/run.vim @@ -0,0 +1,9 @@ +source $KIBI2_REPO_ROOT/tests/common.vim + +" ===== BASIC ===== +new + +CASE : example demo +lua require "run" + +call Snapshot({ 'desc': 'example demo' }) diff --git a/tests/cases/example/on_event/skip-ci b/tests/cases/example/on_event/skip-ci new file mode 100644 index 0000000..e69de29 From 26667a617a00b8a984232324873898158e1910fe Mon Sep 17 00:00:00 2001 From: OGAWA Keiji <70509917+kibi2@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:23:14 +0900 Subject: [PATCH 3/3] docs: update README --- README.md | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 86c5d8c..5271f4f 100644 --- a/README.md +++ b/README.md @@ -300,9 +300,9 @@ All timing values are in milliseconds. ## Examples -Two small examples are included with `keyevent.nvim`. +Several examples are included with `keyevent.nvim`, ranging from basic event handling to more advanced key sequence matching. -### `on_event` +### `on_event()` Use `on_event()` to react to the generated event stream. @@ -310,25 +310,37 @@ Use `on_event()` to react to the generated event stream. require("example.on_event") ``` -The example makes `kj` leave Insert mode. +This example lets you leave Insert mode by typing `kj`. -### `keymap_event` +### `keymap_event()` -Use `keymap_event()` from an expression mapping. +Use `keymap_event()` in an expression mapping. ```lua require("example.keymap") ``` -The example uses `n` normally and shows the search history when `n` is held. +This example behaves like the normal `n` command when `n` is tapped, and displays the search history when `n` is held. + +### `match()` + +Use `match()` to check whether recent key events match a specified sequence. + +```lua +require("example.demo_scheme") +``` + +This example demonstrates how to use `match()` to control color scheme switching and a slideshow. + +To open the help window, press `H` in this sequence: click, tap, click, tap. Then follow the instructions displayed in the window. ### `rush.nvim` -A slightly more complex example is [`rush.nvim`](https://github.com/kibi2/rush.nvim). +A more advanced example is [`rush.nvim`](https://github.com/kibi2/rush.nvim). -It changes the cursor movement distance depending on how a key is typed. +It changes the cursor movement distance based on how a motion key is pressed. -For example, the meaning of a motion key can change depending on whether it is tapped repeatedly or held. +For example, repeated taps and holding a key can trigger different movement behaviors. ## Why?