1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-28 00:50:05 +08:00
SpaceVim/lua/spacevim/api/prompt.lua

184 lines
5.9 KiB
Lua
Raw Normal View History

2022-07-10 20:51:19 +08:00
--=============================================================================
-- prompt.lua --- prompt api for spacevim
2023-03-26 13:50:22 +08:00
-- Copyright (c) 2016-2023 Wang Shidong & Contributors
2022-07-10 20:51:19 +08:00
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local Key = require('spacevim.api').import('vim.keys')
local M = {}
M.__cmp = require('spacevim.api').import('vim.compatible')
M.__vim = require('spacevim.api').import('vim')
M._keys = {
2022-10-04 22:15:08 +08:00
close = Key.t('<Esc>')
2022-07-10 20:51:19 +08:00
}
M._prompt = {
mpt = '==>',
cursor_begin = '',
cursor_char = '',
cursor_end = '',
}
M._function_key = {}
M._quit = true
M._handle_fly = ''
M._onclose = ''
M._oninputpro = ''
function M.open()
M._quit = false
M._build_prompt()
if M.__cmp.fn.empty(M._prompt.cursor_begin) == 0 then
M._handle_input(M._prompt.cursor_begin)
else
M._handle_input()
end
end
function M._c_r_mode_off(timer)
M._c_r_mode = false
end
function M._handle_input(...)
local argv = {...}
local begin = argv[1] or ''
2022-10-04 21:42:03 +08:00
if begin ~= '' then
2022-07-10 20:51:19 +08:00
if type(M._oninputpro) == 'function' then
M._oninputpro()
end
if type(M._handle_fly) == 'function' then
M._handle_fly(M._prompt.cursor_begin
.. M._prompt.cursor_char
.. M._prompt.cursor_end)
end
M._build_prompt()
end
M._c_r_mode = false
while not M._quit do
local char = M.__vim.getchar()
if M._function_key[char] ~= nil then
local ok, rst = pcall(M._function_key[char])
if not ok then print(rst) end
2022-07-10 20:51:19 +08:00
goto continue
end
2022-10-05 21:05:13 +08:00
if M._c_r_mode then
2022-10-05 21:16:35 +08:00
if char:match('^[%w":+/]$') then
2022-10-05 21:05:13 +08:00
local reg = '@' .. char
local paste = vim.fn.get(vim.fn.split(vim.fn.eval(reg), "\n"), 0, '')
M._prompt.cursor_begin = M._prompt.cursor_begin .. paste
M._prompt.cursor_char = vim.fn.matchstr(M._prompt.cursor_end, '.$')
M._c_r_mode = false
M._build_prompt()
else
M._c_r_mode = false
goto continue
end
2022-07-10 20:51:19 +08:00
elseif char == Key.t('<c-r>') then
2022-10-05 09:40:18 +08:00
M._c_r_mode = true
vim.fn.timer_start(2000, M._c_r_mode_off)
M._build_prompt()
goto continue
2022-07-10 20:51:19 +08:00
elseif char == Key.t('<right>') then
M._prompt.cursor_begin = M._prompt.cursor_begin .. M._prompt.cursor_char
2022-10-05 21:19:34 +08:00
M._prompt.cursor_char = M.__cmp.fn.matchstr(M._prompt.cursor_end, '^.')
2022-07-10 20:51:19 +08:00
M._prompt.cursor_end = M.__cmp.fn.substitute(M._prompt.cursor_end, '^.', '', 'g')
M._build_prompt()
goto continue
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<left>') then
2022-10-05 09:40:18 +08:00
if M._prompt.cursor_begin ~= '' then
M._prompt.cursor_end = M._prompt.cursor_char .. M._prompt.cursor_end
2022-10-05 21:19:34 +08:00
M._prompt.cursor_char = vim.fn.matchstr(M._prompt.cursor_begin, '.$')
M._prompt.cursor_begin = vim.fn.substitute(M._prompt.cursor_begin, '.$', '', 'g')
M._build_prompt()
2022-10-05 09:40:18 +08:00
end
goto continue
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<C-w>') then
2022-10-05 21:05:13 +08:00
M._prompt.cursor_begin = M.__cmp.fn.substitute(M._prompt.cursor_begin, [[[^\ .*]\+\s*$]],'','g')
M._build_prompt()
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<C-a>') or char == Key.t('<Home>') then
2022-10-05 21:05:13 +08:00
M._prompt.cursor_end = M.__cmp.fn.substitute(M._prompt.cursor_begin .. M._prompt.cursor_char .. M._prompt.cursor_end, '^.', '', 'g')
M._prompt.cursor_char = M.__cmp.matchstr(M._prompt.cursor_begin, '^.')
M._prompt.cursor_begin = ''
M._build_prompt()
goto continue
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<C-e>') or char == Key.t('<End>') then
2022-10-05 21:05:13 +08:00
M._prompt.cursor_begin = M._prompt.cursor_begin .. M._prompt.cursor_char .. M._prompt.cursor_end
M._prompt.cursor_char = ''
M._prompt.cursor_end = ''
M._build_prompt()
goto continue
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<C-u>') then
2022-10-05 21:05:13 +08:00
M._prompt.cursor_begin = ''
M._build_prompt()
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<C-k>') then
2022-10-05 21:05:13 +08:00
M._prompt.cursor_char = ''
M._prompt.cursor_end = ''
M._build_prompt()
2022-10-04 22:15:08 +08:00
elseif char == Key.t('<bs>') then
M._prompt.cursor_begin = vim.fn.substitute(M._prompt.cursor_begin, '.$', '', 'g')
M._build_prompt()
2022-10-04 22:15:08 +08:00
elseif (type(M._keys.close) == 'string' and char == M._keys.close)
or (type(M._keys.close) == 'table' and vim.fn.index(M._keys.close, char) > -1 ) then
M.close()
break
elseif char == Key.t('<FocusLost>') or char == Key.t('<FocusGained>') or vim.fn.char2nr(char) == 128 then
2022-10-05 21:05:13 +08:00
goto continue
2022-07-10 20:51:19 +08:00
else
M._prompt.cursor_begin = M._prompt.cursor_begin .. char
M._build_prompt()
2022-07-10 20:51:19 +08:00
end
2022-10-04 22:15:08 +08:00
if type(M._oninputpro) == 'function' then
M._oninputpro()
2022-07-10 20:51:19 +08:00
end
2022-10-04 22:15:08 +08:00
if type(M._handle_fly) == 'function' then
2022-10-04 21:42:03 +08:00
M._handle_fly(M._prompt.cursor_begin
.. M._prompt.cursor_char
.. M._prompt.cursor_end)
end
::continue::
end
end
2022-07-10 20:51:19 +08:00
2022-10-04 21:42:03 +08:00
function M._build_prompt()
local ident = M.__cmp.fn['repeat'](' ', M.__cmp.win_screenpos(0)[2] - 1)
vim.cmd('redraw')
vim.api.nvim_echo({
{ident .. M._prompt.mpt, 'Comment'},
{M._prompt.cursor_begin, 'None'},
{M._prompt.cursor_char, 'Wildmenu'},
{M._prompt.cursor_end, 'None'},
{'_', 'Comment'}
}, false, {})
2022-10-04 21:42:03 +08:00
end
2022-07-10 20:51:19 +08:00
2022-10-04 21:42:03 +08:00
function M._clear_prompt()
M._prompt = {
mpt = M._prompt.mpt,
cursor_begin = '',
cursor_char = '',
cursor_end = ''
}
end
2022-07-10 20:51:19 +08:00
2022-10-04 21:42:03 +08:00
function M.close()
2022-10-04 22:15:08 +08:00
if type(M._onclose) == 'function' then
M._onclose()
end
M._clear_prompt()
M._quit = true
2022-10-04 21:42:03 +08:00
end
2022-07-10 20:51:19 +08:00
2022-10-04 21:42:03 +08:00
return M