From ff00585c7c9e0cac8d1c75a8cb60fd82519b6856 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 12 Mar 2024 17:55:15 +0000 Subject: strategy -> col_selectors --- lua/warp.lua | 2 +- lua/warp/col_selectors/grid.lua | 91 ++++++++++++++++++++++++++++++++++++++++ lua/warp/col_selectors/null.lua | 12 ++++++ lua/warp/col_selectors/words.lua | 77 ++++++++++++++++++++++++++++++++++ lua/warp/strategy/grid.lua | 91 ---------------------------------------- lua/warp/strategy/null.lua | 12 ------ lua/warp/strategy/words.lua | 77 ---------------------------------- 7 files changed, 181 insertions(+), 181 deletions(-) create mode 100644 lua/warp/col_selectors/grid.lua create mode 100644 lua/warp/col_selectors/null.lua create mode 100644 lua/warp/col_selectors/words.lua delete mode 100644 lua/warp/strategy/grid.lua delete mode 100644 lua/warp/strategy/null.lua delete mode 100644 lua/warp/strategy/words.lua (limited to 'lua') diff --git a/lua/warp.lua b/lua/warp.lua index b5cb51c..1b0141d 100644 --- a/lua/warp.lua +++ b/lua/warp.lua @@ -84,7 +84,7 @@ end M.run = function(col_selector, win_selector) if not col_selector then - col_selector = require('warp.strategy.grid').strategy + col_selector = require('warp.col_selectors.grid').strategy end if not win_selector then diff --git a/lua/warp/col_selectors/grid.lua b/lua/warp/col_selectors/grid.lua new file mode 100644 index 0000000..bc37b56 --- /dev/null +++ b/lua/warp/col_selectors/grid.lua @@ -0,0 +1,91 @@ +local vim = assert(vim) + +local M = {} + +local hsel1 = "tnshrdlcmwfpgkbvjxy" -- 19 +local hsel2 = "aeiouzq" -- 7 + +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[c .. v] then break end + col_map[c .. v] = col + end + c = char_at(hsel2, i) + col = col + 1 + if col_map[v .. c] then break end + col_map[v .. c] = col + line = line .. v .. c + col = col + 1 + i = i + 1 + end + + return line, col_map +end + +local big_line, col_map = make_big_line() + +M.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/col_selectors/null.lua b/lua/warp/col_selectors/null.lua new file mode 100644 index 0000000..eb00921 --- /dev/null +++ b/lua/warp/col_selectors/null.lua @@ -0,0 +1,12 @@ +local vim = assert(vim) + +local M = {} + +M.null_strategy = function() + return { + display = function() return nil end, + on_char = function() return false end + } +end + +return M diff --git a/lua/warp/col_selectors/words.lua b/lua/warp/col_selectors/words.lua new file mode 100644 index 0000000..8135a45 --- /dev/null +++ b/lua/warp/col_selectors/words.lua @@ -0,0 +1,77 @@ +local vim = assert(vim) + +local M = {} +local alphabet = "etansihrdlocumwfgypkbvjxqz" + +local function split_lines(line) + local i + local j = 1 + + local chars_to_col = {} + local col_to_chars = {} + + local run_regex = function(rx, off) + if #alphabet < j then return end + + if not off then off = 0 end + + i = 0 + while true do + i = string.find(line, rx, i) + if i == nil then break end + i = i + 1 + + if #alphabet < j then return end + + local nextalph = alphabet:sub(j, j) + j = j + 1 + chars_to_col[nextalph] = i + off + col_to_chars[i + off] = nextalph + end + end + + run_regex("%W%w") + run_regex("%s%S") + run_regex("%w%W", -1) + run_regex("[a-z][A-Z]") + + local winstr = "" + i = 1 + while i <= #line do + if col_to_chars[i] then + winstr = winstr .. col_to_chars[i] + else + winstr = winstr .. " " + end + i = i + 1 + end + + return winstr, chars_to_col +end + +M.words_strategy = function() + local chartab + + return { + display = function() + local curpos = vim.api.nvim_win_get_cursor(0) + local line_at = vim.fn.getline(curpos[1]) + local str, tab = split_lines(line_at) + chartab = tab + return str + end, + + on_char = function(ch) + if ch == '$' or ch == '^' then + vim.cmd("normal! " .. ch) + elseif not ch:match('[a-z]') then + vim.cmd("normal! f" .. ch) + else + if chartab[ch] then vim.cmd("norma! " .. chartab[ch] .. "|") end + end + return false + end + } +end + +return M diff --git a/lua/warp/strategy/grid.lua b/lua/warp/strategy/grid.lua deleted file mode 100644 index bc37b56..0000000 --- a/lua/warp/strategy/grid.lua +++ /dev/null @@ -1,91 +0,0 @@ -local vim = assert(vim) - -local M = {} - -local hsel1 = "tnshrdlcmwfpgkbvjxy" -- 19 -local hsel2 = "aeiouzq" -- 7 - -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[c .. v] then break end - col_map[c .. v] = col - end - c = char_at(hsel2, i) - col = col + 1 - if col_map[v .. c] then break end - col_map[v .. c] = col - line = line .. v .. c - col = col + 1 - i = i + 1 - end - - return line, col_map -end - -local big_line, col_map = make_big_line() - -M.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/null.lua b/lua/warp/strategy/null.lua deleted file mode 100644 index eb00921..0000000 --- a/lua/warp/strategy/null.lua +++ /dev/null @@ -1,12 +0,0 @@ -local vim = assert(vim) - -local M = {} - -M.null_strategy = function() - return { - display = function() return nil end, - on_char = function() return false end - } -end - -return M diff --git a/lua/warp/strategy/words.lua b/lua/warp/strategy/words.lua deleted file mode 100644 index 8135a45..0000000 --- a/lua/warp/strategy/words.lua +++ /dev/null @@ -1,77 +0,0 @@ -local vim = assert(vim) - -local M = {} -local alphabet = "etansihrdlocumwfgypkbvjxqz" - -local function split_lines(line) - local i - local j = 1 - - local chars_to_col = {} - local col_to_chars = {} - - local run_regex = function(rx, off) - if #alphabet < j then return end - - if not off then off = 0 end - - i = 0 - while true do - i = string.find(line, rx, i) - if i == nil then break end - i = i + 1 - - if #alphabet < j then return end - - local nextalph = alphabet:sub(j, j) - j = j + 1 - chars_to_col[nextalph] = i + off - col_to_chars[i + off] = nextalph - end - end - - run_regex("%W%w") - run_regex("%s%S") - run_regex("%w%W", -1) - run_regex("[a-z][A-Z]") - - local winstr = "" - i = 1 - while i <= #line do - if col_to_chars[i] then - winstr = winstr .. col_to_chars[i] - else - winstr = winstr .. " " - end - i = i + 1 - end - - return winstr, chars_to_col -end - -M.words_strategy = function() - local chartab - - return { - display = function() - local curpos = vim.api.nvim_win_get_cursor(0) - local line_at = vim.fn.getline(curpos[1]) - local str, tab = split_lines(line_at) - chartab = tab - return str - end, - - on_char = function(ch) - if ch == '$' or ch == '^' then - vim.cmd("normal! " .. ch) - elseif not ch:match('[a-z]') then - vim.cmd("normal! f" .. ch) - else - if chartab[ch] then vim.cmd("norma! " .. chartab[ch] .. "|") end - end - return false - end - } -end - -return M -- cgit