summaryrefslogtreecommitdiff
path: root/lua/warp.lua
blob: b123fdbe7ce8f8e44d45371be96ff330675b8c59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
local vim = assert(vim)
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)
end

M.open_vertical = function()
  local current_win = vim.api.nvim_get_current_win()
  local height = vim.api.nvim_win_get_height(current_win)
  local topline = vim.fn.line('w0')

  M.row_map = {}

  local bufnr =  vim.api.nvim_create_buf(0, 1)
  local line = 0
  local text = {}
  while line < height do
    local t = char_at(cons, line) .. char_at(vowel, line)
    table.insert(text, ' ' .. t)
    M.row_map[t] = topline + line
    line = line + 1
  end
  vim.api.nvim_buf_set_lines(bufnr, 0, -1, 0, text)

  local vert_win = vim.api.nvim_open_win(
     bufnr,
     false,
     {
       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
  vim.api.nvim_win_set_option(vert_win, "signcolumn", "no") -- Hide sign column

  return vert_win
end

M.open_horiz = function()
  local current_win = vim.api.nvim_get_current_win()
  local curpos = vim.api.nvim_win_get_cursor(0)
  local topline = vim.fn.line('w0')

  local width = vim.api.nvim_win_get_width(current_win)
  local width_of_garbage = vim.fn.getwininfo(current_win)[1].textoff

  M.horiz_bufnr =  vim.api.nvim_create_buf(0, 1)
  local col = 0
  -- local bottom_line = ""
  -- local top_line = ""
  local line = ''

  local v = ''
  local c = nil
  local i = 0
  M.col_map = {}
  while col < width do
    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
    c = char_at(hsel2, i)
    if M.col_map [v .. c] then
      break
    end
    M.col_map[v .. c] = col
    line = line .. v .. c
    col = col + 2
    i = i + 1
  end
  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(
     M.horiz_bufnr,
     false,
     {
       relative = 'win',
       row = curpos[1] - topline + 1,
       col = width_of_garbage, -- -curpos[2],
       width = width - width_of_garbage,
       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
  vim.api.nvim_win_set_option(horiz_win, "wrap", false) -- Disable wrapping
  vim.api.nvim_win_set_option(horiz_win, "signcolumn", "no") -- Hide sign column

  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)

  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()
        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)
  end)
end

return M