Continue translating

pull/3/head
Iron-E 4 years ago
parent f70704ea6a
commit 7df77f13fc
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22

@ -5,9 +5,15 @@
--]]
local globals = {}
globals.ESC_NR = 27
globals.TYPE_FUNC = 'function'
globals.TYPE_NUM = 'number'
globals.TYPE_STR = 'string'
globals.TYPE_TBL = 'table'
globals.VIM_FALSE = 0
globals.VIM_TRUE = 1
--[[
/*
* PUBLICIZE MODULE

@ -4,7 +4,8 @@
*/
--]]
local api = vim.api
local api = vim.api
local globals = require('libmodal/src/base/globals')
--[[
/*
@ -13,7 +14,7 @@ local api = vim.api
--]]
local ParseTable = {}
local strings = {} -- not to be returned.
local strings = {} -- not to be returned. Used for split() function.
--[[
/*
@ -29,7 +30,6 @@ ParseTable.CR = 13
* f(x)
*/
--]]
function strings.split(str, pattern)
local split = {}
for char in string.gmatch(str, pattern) do
@ -77,7 +77,7 @@ function ParseTable:new(userTable)
-- run update() again
update(dict[k], splitKey)
-- If dict[k] is a pre-existing table, don't clobber the table— clobber the `CR` value.
elseif type(dict[k]) == 'table' then
elseif type(dict[k]) == globals.TYPE_TBL then
dict[k][ParseTable.CR] = value
-- If dict[k] is not a table, go ahead and clobber the value.
else

@ -19,12 +19,24 @@ local vars = utils.vars
local mode = {}
mode.ParseTable = require('libmodal/src/mode/ParseTable')
------------------------------------
--[[ SUMMARY:
* Parse the `comboDict` and see if there is any command to execute.
]]
--[[ PARAMS:
* `modeName` => the name of the mode that is currently active.
]]
------------------------------------
function mode._comboSelect(modeName)
local comboDict = vars.combos.instances[modeName]
-- TODO translate `LibmodalEnterWithCombos`
end
------------------------
--[[ SUMMARY:
* Enter a mode.
]]
--[[ PARAMETERS:
--[[ PARAMS:
* `args[1]` => the mode name.
* `args[2]` => the mode callback, or mode combo table.
* `args[3]` => optional exit supresion flag.
@ -37,32 +49,22 @@ function mode.enter(...)
-- Create the indicator for the mode.
local indicator = utils.Indicator:new(args[1])
-- Grab the state of the window.
local winState = utils.WindowState.new()
-- Convert the name into one that can be used for variables.
local modeName = string.lower(args[1])
-- Determine whether or not this function should handle exiting automatically.
local handleExitEvents = false
if #args > 2 and args[3] then
handleExitEvents = true
end
-- Determine whether a callback was specified, or a combo table.
if type(args[2]) == 'table' then
-- Placeholder for timeout value.
local doTimeout = nil
-- Read the correct timeout variable.
if api.nvim_exists('g', vars.timeout.name(modeName)) then
doTimeout = vars.nvim_get(vars.timeout, modeName)
else
doTimeout = vars.libmodalTimeout
end
vars.timeout.instances[modeName] = doTimeout
-- Build the parse tree.
vars.combos.instances[modeName] = mode.ParseTable:new(args[2])
-- Initialize the input history variable.
vars.input.instances[modeName] = {}
-- Determine whether a callback was specified, or a combo table.
if type(args[2]) == globals.TYPE_TBL then
mode._initTimeouts(modeName)
end
--[[ MODE LOOP. ]]
@ -70,28 +72,71 @@ function mode.enter(...)
while true do
-- Try (using pcall) to use the mode.
local noErrors = pcall(function()
-- TODO: write main loop.
-- If the mode is not handling exit events automatically and the global exit var is true.
if not handleExitEvents and var.nvim_get(vars.exit, modeName) then
break
end
-- Echo the indicator.
api.nvim_lecho(indicator)
-- Capture input.
local uinput = api.nvim_input()
vars.nvim_set(vars.input, modeName, uinput)
-- Make sure that the user doesn't want to exit.
if handleExitEvents and uinput == globals.ESC_NR then
break
-- If the second argument was a dict, parse it.
elseif type(args[2]) == globals.TYPE_TBL then
mode._comboSelect(modeName)
-- If the second argument was a function, execute it.
else
args[2]()
end
end)()
-- If there were errors, handle them.
if not noErrors then
api.nvim_bell()
api.nvim_show_err( 'vim-libmodal error',
api.nvim_get_vvar('throwpoint')
.. '\n' ..
api.nvim_get_vvar('exception')
)
mode._showError()
break
end
end
--[[ TEARDOWN. ]]
api.nvim_redraw()
api.nvim_echo('')
api.nvim_command('call garbagecollect()')
winState:restore()
end
function mode._initTimeouts(modeName)
-- Placeholder for timeout value.
local doTimeout = nil
-- Read the correct timeout variable.
if api.nvim_exists('g', vars.timeout.name(modeName)) then
doTimeout = vars.nvim_get(vars.timeout, modeName)
else
doTimeout = vars.libmodalTimeout
end
vars.timeout.instances[modeName] = doTimeout
-- Build the parse tree.
vars.combos.instances[modeName] = mode.ParseTable:new(args[2])
-- Initialize the input history variable.
vars.input.instances[modeName] = {}
end
--[[ TODO: translate these:
call s:Restore(l:winState)
mode | echo ''
call garbagecollect()
]]
function mode._showError()
api.nvim_bell()
api.nvim_show_err( 'vim-libmodal error',
api.nvim_get_vvar('throwpoint')
.. '\n' ..
api.nvim_get_vvar('exception')
)
end
--[[

@ -7,7 +7,7 @@ local Entry = {}
--[[
/*
* STRUCT `Entry`
* CLASS `Entry`
*/
--]]

@ -14,7 +14,7 @@ local Indicator = {}
--[[
/*
* STRUCT `Indicator`
* CLASS `Indicator`
*/
--]]

@ -12,17 +12,30 @@ local api = vim.api
--]]
local WindowState = {}
local height = 'winheight'
local width = 'winwidth'
--[[
/*
* STRUCT `WindowState`
* CLASS `WindowState`
*/
--]]
function WindowState.new()
return {
['height'] = api.nvim_get_option('winheight'),
['width'] = api.nvim_get_option('winwidth')
local winState = {
['height'] = api.nvim_get_option(height),
['width'] = api.nvim_get_option(width),
}
function winState:restore()
api.nvim_set_option(height, self['height'])
api.nvim_set_option(width, self['width'])
end
return winState
end
function WindowState.restore(state)
end
--[[

@ -25,6 +25,18 @@ function api.nvim_bell()
api.nvim_command('normal ' .. escape)
end
---------------------------
--[[ SUMMARY:
* Echo a string to Vim.
]]
--[[ PARAMS:
* `str` => the string to echo.
]]
---------------------------
function api.nvim_echo(str)
api.nvim_command("echo " .. tostring(str))
end
-----------------------------------
--[[ SUMMARY:
* Check whether or not some variable exists.
@ -38,6 +50,15 @@ function api.nvim_exists(scope, var)
return api.nvim_eval("exists('" .. scope .. ":" .. var .. "')") ~= globals.VIM_FALSE
end
-------------------------
--[[ SUMMARY:
* Gets one character of user input, as a number.
]]
-------------------------
function api.nvim_input()
return api.nvim_eval('getchar()')
end
------------------------
--[[ SUMMARY:
* Echo a table of {`hlgroup`, `str`} tables.

@ -64,7 +64,11 @@ end
]]
------------------------------------
function vars.nvim_get(var, modeName)
return api.nvim_get_vars(var.name(modeName))
return api.nvim_get_var(var.name(modeName))
end
function vars.nvim_set(var, modeName, val)
api.nvim_set_var(var.name(modeName), val)
end
--[[
@ -73,6 +77,7 @@ end
*/
--]]
new('combos' , 'ModeCombos')
new('exit' , 'ModeExit')
new('input' , 'ModeInput')
new('timeout' , 'ModeTimeout')

Loading…
Cancel
Save