ivy.nvim/lua/ivy/prompt.lua
Ade Attwood c9ce8ac4d1 feat: implement emacs bindings for the prompt like bash
Now the prompt will act like the default bash readline with emacs key
bindings, clear and delete word.

You can now also move left and right in the prompt to insert chars in
the middle of the prompt rather than having to delete your search term
and start again.
2022-07-24 12:50:30 +01:00

60 lines
1.4 KiB
Lua

-- The prefix that will be before the search text for the user
local prompt_prefix = ">> "
local prompt = {}
prompt.suffix = ""
prompt.value = ""
prompt.text = function()
return prompt.value .. prompt.suffix
end
prompt.update = function()
vim.api.nvim_echo({
{ prompt_prefix, "None" },
{ prompt.value:sub(1, -2), "None" },
{ prompt.value:sub(-1, -1), "Underlined" },
{ prompt.suffix, "None" },
}, false, {})
end
prompt.input = function(char)
if char == "BACKSPACE" then
prompt.value = string.sub(prompt.value, 0, -2)
elseif char == "LEFT" then
if #prompt.value > 0 then
prompt.suffix = prompt.value:sub(-1, -1) .. prompt.suffix
prompt.value = prompt.value:sub(1, -2)
end
elseif char == "RIGHT" then
if #prompt.suffix > 0 then
prompt.value = prompt.value .. prompt.suffix:sub(1, 1)
prompt.suffix = prompt.suffix:sub(2, -1)
end
elseif char == "DELETE_WORD" then
prompt.value = prompt.value:match "(.*)%s+.*$"
if prompt.value == nil then
prompt.value = ""
end
elseif char == "\\\\" then
prompt.value = prompt.value .. "\\"
else
prompt.value = prompt.value .. char
end
prompt.update()
end
prompt.set = function(value)
prompt.value = value
prompt.suffix = ""
prompt.update()
end
prompt.destroy = function()
prompt.value = ""
prompt.suffix = ""
vim.notify ""
end
return prompt