From 7df77f13fc151e5a9e9e165202c7c7dcbe5eef66 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 6 May 2020 01:36:52 -0400 Subject: [PATCH] Continue translating --- lua/libmodal/src/base/globals.lua | 6 ++ lua/libmodal/src/mode/ParseTable/init.lua | 8 +- lua/libmodal/src/mode/init.lua | 107 ++++++++++++++------ lua/libmodal/src/utils/Indicator/Entry.lua | 2 +- lua/libmodal/src/utils/Indicator/init.lua | 2 +- lua/libmodal/src/utils/WindowState/init.lua | 21 +++- lua/libmodal/src/utils/api.lua | 21 ++++ lua/libmodal/src/utils/vars.lua | 7 +- 8 files changed, 132 insertions(+), 42 deletions(-) diff --git a/lua/libmodal/src/base/globals.lua b/lua/libmodal/src/base/globals.lua index 9147f85..8fef1e6 100644 --- a/lua/libmodal/src/base/globals.lua +++ b/lua/libmodal/src/base/globals.lua @@ -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 diff --git a/lua/libmodal/src/mode/ParseTable/init.lua b/lua/libmodal/src/mode/ParseTable/init.lua index 51f6ffd..341fcee 100644 --- a/lua/libmodal/src/mode/ParseTable/init.lua +++ b/lua/libmodal/src/mode/ParseTable/init.lua @@ -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 diff --git a/lua/libmodal/src/mode/init.lua b/lua/libmodal/src/mode/init.lua index 1473bad..b9aa6e4 100644 --- a/lua/libmodal/src/mode/init.lua +++ b/lua/libmodal/src/mode/init.lua @@ -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 --[[ diff --git a/lua/libmodal/src/utils/Indicator/Entry.lua b/lua/libmodal/src/utils/Indicator/Entry.lua index 629077d..0c8a916 100644 --- a/lua/libmodal/src/utils/Indicator/Entry.lua +++ b/lua/libmodal/src/utils/Indicator/Entry.lua @@ -7,7 +7,7 @@ local Entry = {} --[[ /* - * STRUCT `Entry` + * CLASS `Entry` */ --]] diff --git a/lua/libmodal/src/utils/Indicator/init.lua b/lua/libmodal/src/utils/Indicator/init.lua index 8484c7c..a473e73 100644 --- a/lua/libmodal/src/utils/Indicator/init.lua +++ b/lua/libmodal/src/utils/Indicator/init.lua @@ -14,7 +14,7 @@ local Indicator = {} --[[ /* - * STRUCT `Indicator` + * CLASS `Indicator` */ --]] diff --git a/lua/libmodal/src/utils/WindowState/init.lua b/lua/libmodal/src/utils/WindowState/init.lua index 671c527..8de82e9 100644 --- a/lua/libmodal/src/utils/WindowState/init.lua +++ b/lua/libmodal/src/utils/WindowState/init.lua @@ -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 --[[ diff --git a/lua/libmodal/src/utils/api.lua b/lua/libmodal/src/utils/api.lua index 068542e..b1f1978 100644 --- a/lua/libmodal/src/utils/api.lua +++ b/lua/libmodal/src/utils/api.lua @@ -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. diff --git a/lua/libmodal/src/utils/vars.lua b/lua/libmodal/src/utils/vars.lua index d1929f1..1fac054 100644 --- a/lua/libmodal/src/utils/vars.lua +++ b/lua/libmodal/src/utils/vars.lua @@ -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')