summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-03-09 17:52:31 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-03-09 17:52:31 -0700
commit894306991ff8bf2313da5df3c7782c2d1719e72e (patch)
tree1bcd711da01e49453a2f36e9b0aa99135c559718
parent2f8ba5cfd60d253934957644aa40630b5d5e7729 (diff)
downloadnvim-warp-894306991ff8bf2313da5df3c7782c2d1719e72e.tar.gz
nvim-warp-894306991ff8bf2313da5df3c7782c2d1719e72e.tar.bz2
nvim-warp-894306991ff8bf2313da5df3c7782c2d1719e72e.zip
The plugin is roughly usable.
-rw-r--r--lua/warp.lua156
1 files changed, 69 insertions, 87 deletions
diff --git a/lua/warp.lua b/lua/warp.lua
index 9537bcb..b123fdb 100644
--- a/lua/warp.lua
+++ b/lua/warp.lua
@@ -4,6 +4,9 @@ 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)
@@ -11,7 +14,6 @@ end
M.open_vertical = function()
local current_win = vim.api.nvim_get_current_win()
- local curpos = vim.api.nvim_win_get_cursor(0)
local height = vim.api.nvim_win_get_height(current_win)
local topline = vim.fn.line('w0')
@@ -32,13 +34,14 @@ M.open_vertical = function()
bufnr,
false,
{
- relative = 'cursor',
- row = -curpos[1],
- col = -4,
+ relative = 'win',
+ row = 0,
+ col = 0,
width = 4,
height = height
})
+ vim.api.nvim_win_set_option(vert_win, "winhighlight", "Normal:Salmon")
vim.api.nvim_win_set_option(vert_win, "number", false) -- Hide line numbers
vim.api.nvim_win_set_option(vert_win, "relativenumber", false) -- Hide relative numbers
vim.api.nvim_win_set_option(vert_win, "wrap", false) -- Disable wrapping
@@ -55,43 +58,47 @@ M.open_horiz = function()
local width = vim.api.nvim_win_get_width(current_win)
local width_of_garbage = vim.fn.getwininfo(current_win)[1].textoff
- local bufnr = vim.api.nvim_create_buf(0, 1)
+ M.horiz_bufnr = vim.api.nvim_create_buf(0, 1)
local col = 0
- local bottom_line = ""
- local top_line = ""
- local vi = 0
+ -- local bottom_line = ""
+ -- local top_line = ""
+ local line = ''
local v = ''
+ local c = nil
+ local i = 0
M.col_map = {}
while col < width do
- local c = char_at(cons, col)
- bottom_line = bottom_line .. c
- if (col % #cons) == 0 then
- v = char_at(vowel, vi)
- vi = vi + 1
- top_line = top_line .. v
- else
- top_line = top_line .. ' '
+ v = char_at(hsel1, i)
+ if c then
+ if M.col_map [c .. v] then
+ break
+ end
+ M.col_map[c .. v] = col - 1
end
- if M.col_map[v .. c] then
+ c = char_at(hsel2, i)
+ if M.col_map [v .. c] then
break
end
-
M.col_map[v .. c] = col
- col = col + 1
+ line = line .. v .. c
+ col = col + 2
+ i = i + 1
end
- vim.api.nvim_buf_set_lines(bufnr, 0, -1, 0, { top_line, bottom_line })
+ vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { line })
+ M.horiz_line = line
local horiz_win = vim.api.nvim_open_win(
- bufnr,
+ M.horiz_bufnr,
false,
{
relative = 'win',
row = curpos[1] - topline + 1,
col = width_of_garbage, -- -curpos[2],
width = width - width_of_garbage,
- height = 2
+ height = 1
})
+ vim.api.nvim_win_set_option(horiz_win, "winhighlight", "Normal:Salmon")
vim.api.nvim_win_set_option(horiz_win, "number", false) -- Hide line numbers
vim.api.nvim_win_set_option(horiz_win, "relativenumber", false) -- Hide relative numbers
@@ -101,80 +108,55 @@ M.open_horiz = function()
return horiz_win
end
+M.filter_horiz_string = function(ch)
+ local new_horiz_line = ''
+ local i = 1
+ while i <= #M.horiz_line do
+ local curch = char_at(M.horiz_line, i - 1)
+ if curch == ch then
+ new_horiz_line = new_horiz_line .. char_at(M.horiz_line, i)
+ i = i + 1
+ end
+ new_horiz_line = new_horiz_line .. ' '
+ i = i + 1
+ end
+ vim.api.nvim_buf_set_lines(M.horiz_bufnr, 0, -1, 0, { new_horiz_line })
+end
+
+local function next_char(callback)
+ local timer = vim.loop.new_timer()
+ timer:start(0, 0, vim.schedule_wrap(function()
+ local ch = vim.fn.nr2char(vim.fn.getchar())
+ callback(ch)
+ end))
+end
+
M.run = function()
local w1 = M.open_vertical()
local current_pos = vim.api.nvim_win_get_cursor(0)
- -- local w2 = M.open_horiz()
-
- vim.schedule(function ()
- local c1 = vim.fn.nr2char(vim.fn.getchar())
- local c2 = vim.fn.nr2char(vim.fn.getchar())
-
- vim.api.nvim_win_close(w1, 1)
- local r = M.row_map[c1 .. c2]
- if r then
- vim.api.nvim_win_set_cursor(0, {r, current_pos[2]})
- vim.schedule(function()
+ next_char(function(vch1)
+ next_char(function(vch2)
+ vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w1), { force = true })
+ -- vim.api.nvim_win_close(w1, 1)
+ local r = M.row_map[vch1 .. vch2]
+ if r then
+ vim.api.nvim_win_set_cursor(0, {r, current_pos[2]})
local w2 = M.open_horiz()
- local timer = vim.loop.new_timer()
- -- Bug in vim runtime? If I don't use the timer, the window never shows
- -- up.
- timer:start(10, 0, vim.schedule_wrap(function()
- c1 = vim.fn.nr2char(vim.fn.getchar())
- c2 = vim.fn.nr2char(vim.fn.getchar())
- local c = M.col_map[c1 .. c2]
-
- vim.api.nvim_win_close(w2, 1)
-
+ next_char(function (hch1)
+ M.filter_horiz_string(hch1)
+ vim.cmd("redraw!")
+ local hch2 = vim.fn.nr2char(vim.fn.getchar())
+ local c = M.col_map[hch1 .. hch2]
+ vim.api.nvim_buf_delete(vim.api.nvim_win_get_buf(w2), { force = true })
+ -- vim.api.nvim_win_close(w2, 1)
if c then
vim.api.nvim_win_set_cursor(0, {r, c})
end
- end))
- end)
- end
-
- -- vim.schedule(function ()
- -- c1 = vim.fn.nr2char(vim.fn.getchar())
- -- c2 = vim.fn.nr2char(vim.fn.getchar())
-
- -- -- vim.api.nvim_win_close(w2, 1)
- -- end)
+ end)
+ end
+ end)
end)
-
- -- vim.schedule(function()
- -- local c1 = vim.fn.nr2char(vim.fn.getchar())
- -- local c2 = vim.fn.nr2char(vim.fn.getchar())
-
- -- vim.api.nvim_win_close(w, 1)
-
- -- local r = M.row_map[c1 .. c2]
- -- vim.print(c1 .. c2)
- -- vim.print(M.row_map[c1 .. c2])
- -- if r then
- -- -- local b, _, c, o = vim.fn.getpos('.')
- -- -- vim.fn.setpos('.', {b, r, c, o})
-
- -- vim.schedule(function()
- -- vim.print("Open horiz?")
- -- w = M.open_horiz()
- -- end)
-
-
- -- vim.schedule(function ()
- -- -- c1 = vim.fn.getchar()
- -- -- c2 = vim.fn.getchar()
-
- -- vim.api.nvim_win_close(w, 1)
-
- -- -- c = M.col_map[c1 .. c2]
- -- -- if c then
- -- -- b, r, _, o = vim.fn.getpos('.')
- -- -- vim.fn.setpos('.', {b, r, c, o})
- -- -- end
- -- end)
- -- end
- -- end)
end
return M