summaryrefslogtreecommitdiff
path: root/lua/warp/strategy/words.lua
blob: 8135a459ffc7ea3b988ab9c6569e4eb0b3f0568b (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
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