From 38356c4b980f70d498e211ef686d4d6a6c003dc1 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 10 Mar 2024 23:59:43 -0600 Subject: default -> grid strategy. --- lua/warp.lua | 3 -- lua/warp/strategy/default.lua | 92 ------------------------------------------- lua/warp/strategy/grid.lua | 92 +++++++++++++++++++++++++++++++++++++++++++ plugin/warp.vim | 16 +++++++- 4 files changed, 107 insertions(+), 96 deletions(-) delete mode 100644 lua/warp/strategy/default.lua create mode 100644 lua/warp/strategy/grid.lua diff --git a/lua/warp.lua b/lua/warp.lua index 563e177..bda64fb 100644 --- a/lua/warp.lua +++ b/lua/warp.lua @@ -4,9 +4,6 @@ local M = {} local cons = "tnshrdlcumwfgypkbvjxqz" -- 21 local vowel = "aeiou" -- 5 -local hsel1 = "tnshrdlcumwfgypkbvjxqz" -- 21 -local hsel2 = "aeiou" -- 5 - local function char_at(s, i) local m = (i % #s) + 1 return string.sub(s, m, m) diff --git a/lua/warp/strategy/default.lua b/lua/warp/strategy/default.lua deleted file mode 100644 index c48bdf3..0000000 --- a/lua/warp/strategy/default.lua +++ /dev/null @@ -1,92 +0,0 @@ -local vim = assert(vim) - -local M = {} - -local hsel1 = "tnshrdlcumwfgypkbvjxqz" -- 21 -local hsel2 = "aeiou" -- 5 - -local function char_at(s, i) - local m = (i % #s) + 1 - return string.sub(s, m, m) -end - -local function make_big_line() - local col = 0 - local line = '' - - local v = '' - local c = nil - local i = 0 - local col_map = {} - while true do - v = char_at(hsel1, i) - if c then - if col_map[v .. c] then break end - col_map[c .. v] = col - end - c = char_at(hsel2, i) - if col_map[c .. v] then break end - line = line .. v - col = col + 1 - line = line .. c - col_map[v .. c] = col - col = col + 1 - i = i + 1 - end - - return line, col_map -end - -local big_line, col_map = make_big_line() - -M.default_strategy = function() - local filter - - return { - display = function() - local curpos = vim.api.nvim_win_get_cursor(0) - local line_at = vim.fn.getline(curpos[1]) - local unfiltered = big_line:sub(1, #line_at + 1) - local line = unfiltered - - if filter then - line = "" - local i = 1 - while i <= #unfiltered do - local curch = char_at(unfiltered, i - 1) - if curch == filter then - line = line .. char_at(unfiltered, i) - else - line = line .. ' ' - end - i = i + 1 - end - end - - return line - end, - - on_char = function(ch) - if not filter then - if ch == '$' or ch == '^' then - vim.cmd("normal! " .. ch) - return false - elseif not ch:match('[a-z]') then - vim.cmd("normal! f" .. ch) - return false - else - filter = ch - return true - end - else - if col_map[filter .. ch] then - vim.cmd("normal! " .. col_map[filter .. ch] .. "|") - end - end - - return false - end - } -end - -return M diff --git a/lua/warp/strategy/grid.lua b/lua/warp/strategy/grid.lua new file mode 100644 index 0000000..2e8a871 --- /dev/null +++ b/lua/warp/strategy/grid.lua @@ -0,0 +1,92 @@ +local vim = assert(vim) + +local M = {} + +local hsel1 = "tnshrdlcumwfgypkbvjxqz" -- 21 +local hsel2 = "aeiou" -- 5 + +local function char_at(s, i) + local m = (i % #s) + 1 + return string.sub(s, m, m) +end + +local function make_big_line() + local col = 0 + local line = '' + + local v = '' + local c = nil + local i = 0 + local col_map = {} + while true do + v = char_at(hsel1, i) + if c then + if col_map[v .. c] then break end + col_map[c .. v] = col + end + c = char_at(hsel2, i) + if col_map[c .. v] then break end + line = line .. v + col = col + 1 + line = line .. c + col_map[v .. c] = col + col = col + 1 + i = i + 1 + end + + return line, col_map +end + +local big_line, col_map = make_big_line() + +M.grid_strategy = function() + local filter + + return { + display = function() + local curpos = vim.api.nvim_win_get_cursor(0) + local line_at = vim.fn.getline(curpos[1]) + local unfiltered = big_line:sub(1, #line_at + 1) + local line = unfiltered + + if filter then + line = "" + local i = 1 + while i <= #unfiltered do + local curch = char_at(unfiltered, i - 1) + if curch == filter then + line = line .. char_at(unfiltered, i) + else + line = line .. ' ' + end + i = i + 1 + end + end + + return line + end, + + on_char = function(ch) + if not filter then + if ch == '$' or ch == '^' then + vim.cmd("normal! " .. ch) + return false + elseif not ch:match('[a-z]') then + vim.cmd("normal! f" .. ch) + return false + else + filter = ch + return true + end + else + if col_map[filter .. ch] then + vim.cmd("normal! " .. col_map[filter .. ch] .. "|") + end + end + + return false + end + } +end + +return M diff --git a/plugin/warp.vim b/plugin/warp.vim index 2190d31..4052622 100644 --- a/plugin/warp.vim +++ b/plugin/warp.vim @@ -1,3 +1,17 @@ -command! WarpGrid lua require('warp').run(require('warp.strategy.default').default_strategy) +" Uses a strategy that allows the user to select an arbitrary line and column by +" using 2x2 key strokes. Is more complex, but most powerful. +command! WarpGrid lua require('warp').run(require('warp.strategy.grid').grid_strategy) + +" Has the user select the column based on nows. command! WarpWords lua require('warp').run(require('warp.strategy.words').words_strategy) + +" Warps to the line and leaves the cursor on the current line. command! WarpLine lua require('warp').run(require('warp.strategy.null').null_strategy) + +noremap (warp-grid) WarpGrid +noremap (warp-words) WarpWords +noremap (warp-line) WarpLine + +onoremap v(warp-grid) WarpGrid +onoremap v(warp-words) WarpWords +onoremap v(warp-line) WarpLine -- cgit